Re: Random not being so random

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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


.



Relevant Pages

  • Re: Looping through Query to create multiple sheets in excel- Just need the loop
    ... I always get to this point and I can't figure out how to loop through ... I have put the string in the query at the bottom. ... Optional strWorkSheet As String, Optional strRange As ... Dim objXLSheet As Object 'Excel.Worksheet ...
    (microsoft.public.access.forms)
  • extension_pack
    ... It is used to set upper loop -- limits for non-deterministic values thus avoiding the use of access -- types and enabling the functions to be used for synthesizeable code. ... DivisorVal: integer) return std_logic_vector; function "/"(DividendVal: string; DivisorVal: integer) return std_logic_vector; ... for loopVar in 0 to slvVal'length/4-1 loop ... end loop; if then return not resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to else return resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to end if; ...
    (comp.lang.vhdl)
  • Re: How is this conditional able to execute?
    ... inside the loop, then each time the loop iterates then a NEW reference to ... Note that the test of returndata is not a type. ... Dim returndata As String = "" ... Not that 'Changing' is only displayed when returndata is NOT an empty string ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How is this conditional able to execute?
    ... Why is this part out of the loop: ... Maybe I just don't understand the returndata = "" because of the byte array. ... How can I read the string in: ... > Dim t As Thread ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How to put lines with certain text (from a file) in an array
    ... In one sense all consistent but now the Filter approach is taking about 2x longer than the loop with Instr with all sizes. ... Dim sFile As String, sText As String ...
    (microsoft.public.excel.programming)