Random not being so random

J

Jeff

I was testing out some code to create a string of random characters.
The code inside of the Button1 Click event was along the lines of:
Dim i as Integer
Dim rGen as New Random()
Dim uniqueName as String
For i = 1 to 10
uniqueName = Chr(rGen.Next(65, 90)) & Chr(rGen.Next(65, 90)) &
Chr(rGen.Next(65, 90))
uniqueName &= Chr(rGen.Next(65, 90)) & Chr(rGen.Next(65, 90)) &
Chr(rGen.Next(65, 90))
Textbox1.Text &= uniqueName & ControlChars.NewLine
Next

That worked great. The textbox would be filled with 10 different strings.

So, I proceeded to create a function would return the random string,
instead of have to use that code where needed. So I throw that into a loop
For i = 1 to 10
Textbox1.text &= GenerateString() &= ControlChars.NewLine
Next

What's that? Ten of the exact same string in a row? Where did all the
randomness go? Ok, instead, let's just call the code ten times in a row...

Textbox1.Text &= GenerateString() &= ControlChars.NewLine
Textbox1.Text &= GenerateString() &= ControlChars.NewLine
Textbox1.Text &= GenerateString() &= ControlChars.NewLine
...... etc etc

Same results. Ten of the same string in a row. Sometimes the first one
or two will be different (I'm guessing it's calculated the number off
the system clock, and it's a millisecond later or something.)

What's the deal? When coded directly into the Sub for the button's
click it returns different values each interation through the loop, but
not when called from a Function?

Thanks,
Jeff
 
J

Jay B. Harlow [MVP - Outlook]

Jeff,
Be certain to read the remarks at:

http://msdn.microsoft.com/library/d...ref/html/frlrfSystemRandomClassctorTopic1.asp

and:

http://msdn.microsoft.com/library/d...ref/html/frlrfSystemRandomClassctorTopic2.asp

Basically your loop is fast enough that the seed that Random is deriving
matches each time through the loop.

Hope this helps
Jay


|I was testing out some code to create a string of random characters.
| The code inside of the Button1 Click event was along the lines of:
| Dim i as Integer
| Dim rGen as New Random()
| Dim uniqueName as String
| For i = 1 to 10
| uniqueName = Chr(rGen.Next(65, 90)) & Chr(rGen.Next(65, 90)) &
| Chr(rGen.Next(65, 90))
| uniqueName &= Chr(rGen.Next(65, 90)) & Chr(rGen.Next(65, 90)) &
| Chr(rGen.Next(65, 90))
| Textbox1.Text &= uniqueName & ControlChars.NewLine
| Next
|
| That worked great. The textbox would be filled with 10 different strings.
|
| So, I proceeded to create a function would return the random string,
| instead of have to use that code where needed. So I throw that into a
loop
| For i = 1 to 10
| Textbox1.text &= GenerateString() &= ControlChars.NewLine
| Next
|
| What's that? Ten of the exact same string in a row? Where did all the
| randomness go? Ok, instead, let's just call the code ten times in a
row...
|
| Textbox1.Text &= GenerateString() &= ControlChars.NewLine
| Textbox1.Text &= GenerateString() &= ControlChars.NewLine
| Textbox1.Text &= GenerateString() &= ControlChars.NewLine
| ..... etc etc
|
| Same results. Ten of the same string in a row. Sometimes the first one
| or two will be different (I'm guessing it's calculated the number off
| the system clock, and it's a millisecond later or something.)
|
| What's the deal? When coded directly into the Sub for the button's
| click it returns different values each interation through the loop, but
| not when called from a Function?
|
| Thanks,
| Jeff
 
L

Lucvdv

So, I proceeded to create a function would return the random string,
instead of have to use that code where needed. So I throw that into a loop
For i = 1 to 10
Textbox1.text &= GenerateString() &= ControlChars.NewLine
Next

What's that? Ten of the exact same string in a row? Where did all the
randomness go? Ok, instead, let's just call the code ten times in a row...

If you have this inside the GenerateString function:

: Dim rGen as New Random()

the behavior is expected.

Use a single static Random at the application level instead.
 
R

Ross Presser

What's the deal? When coded directly into the Sub for the button's
click it returns different values each interation through the loop, but
not when called from a Function?

To combine and amplify the other two explanations in this thread:

Since you create a brand new generator on each call to the function

Dim rGen as New Random()

it will reseed it each time. Since the code runs very fast and the seed
comes off the system clock, the generator will start out with the same
sequence of numbers each time the function is called.

If you use

Static rGen as New Random()

then the generator will be seeded only once, and the sequence of numbers
will be different on each call.

To make it more explicit, if you had done

Dim rGen as New Random(7293467)

then you would *always* get the same text string out of your function on
*every* call.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top