Don C20

VB help says:

"Randomize uses Number to initialize the Rnd function's random-number generator, giving it a new seed value. "

...

then says

" Using Randomize with the same value for Number does not repeat the previous sequence."

I find second statement a bit counterintuitive given first statement. Probably I'm missing something obvious but would be grateful if anyone could explain why the previous sequence is not repeated.



Re: Visual Basic Express Edition Randomize with same value; why is sequence different?

DMan1

Because it is a random number....even if you reseed the generator with the same seed they will not com up with the same sequence of numbers:

For count As Integer = 0 To 9

Randomize(1)

Dim x As Single = Rnd()

Randomize(1)

Dim y As Single = Rnd()

Debug.Print(x & " " & y)

Next






Re: Visual Basic Express Edition Randomize with same value; why is sequence different?

Solitaire

Randomize uses the system timer as the seed value, which keeps changing several times every second and is therefore unpredictable. If you want to repeat the same sequence each time it starts, then you must use a constant seed value. This sample will keep repeating the same sequence each time with the same seed number. To vary the sequence, remove (seed) from the New Random() argument and leave it blank.

Private Sub btnRandom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRandom.Click

Dim rand, seed As Integer

'user enters a seed number in txtSeed textbox

Integer.TryParse(txtSeed.Text, seed)

Dim randnum As Random = New Random(seed)

txtRandom.Clear()

For x As Integer = 1 To 20

rand = randnum.Next(1, 21)

txtRandom.Text &= rand.ToString & " "

Next x

End Sub