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
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