Reputation: 409
I wrote this code below, expecting it to return the same thing each time I ran it, and it did not. May someone explain to me why A specifically seeded Random Number Generator would generate a different number each time I run it, because i wrote the same program as a command line application and it worked just fine.
Here is my VB.NET code (broken code):
'Bunch of WinForms Code
Private Sub PictureBox1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles PictureBox1.Click
Randomize(3)
MsgBox(Rnd.ToString)
End Sub
Here is my working VB.NET Command Line Application:
Imports System.Console
Public Module rnum
Sub Main()
Randomize(3)
WriteLine(Rnd.ToString) 'Returns 0.1387751
End Sub
End Module
Upvotes: 3
Views: 129
Reputation: 1763
What happens if you put the console code into a loop? I'm betting you would receive the same sequence of numbers. The console is running 1 time and producing the first result whereas if you click the button multiple times, you're in essence looping through the randomize results.
When I do this, 4 times I receive the same 4 numbers when I click the button 4 times.
Sub Main()
For x As Integer = 0 To 3
Randomize(3)
Console.WriteLine(Rnd.ToString) 'Returns 0.1387751
Next
Console.ReadLine()
End Sub
produces:
0.1387751
0.05591547
0.8356526
0.2308619
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Randomize(3)
MsgBox(Rnd.ToString)
End Sub
produces
0.1387751 on 1st click
0.05591547 on 2nd click
0.8356526 on 3rd click
0.2308619 on 4th click
Upvotes: 4