Re: Random not being so random
- From: "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@xxxxxxx>
- Date: Thu, 21 Apr 2005 22:53:40 -0500
Jeff,
Be certain to read the remarks at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemRandomClassctorTopic1.asp
and:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/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
"Jeff" <jssignup@xxxxxxxxxx> wrote in message
news:O10ooPtRFHA.356@xxxxxxxxxxxxxxxxxxxxxxx
|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
.
- References:
- Random not being so random
- From: Jeff
- Random not being so random
- Prev by Date: RE: C# Conversion help needed.
- Next by Date: Re: What is the simplest way to link DLLs?
- Previous by thread: Random not being so random
- Next by thread: Re: Random not being so random
- Index(es):
Relevant Pages
|