Robert3234

Hi all,
I've got a sub that I call to load a bunch of random numbers into a few arrays. The random numbers are generated in the array using the:

dim r as new random
r.Next(0, 4)

method, and a for loop cycles the random number generation process a few times...

When i put a messagebox in the middle of the function's for loop (before it actually loads the numbers into the arrays), different instances of the same array are truly different (which is the way it should be), and when I don't use the messagebox, even though the numbers were randomly generated moments before, they are all the same between instances....

It's really weird! Does anyone have any ideas that could explain this behaviour

I'm sorry if I'm not being specific enough... if you can't understand the gist of my problem, or need some more sample code, just let me know!

Thanks for your input... I'm really stumped, and am working on a school project that's due in a few days (so i need to figure this out soon!)....

Thanks so much

-Robert



Re: Visual Basic Express Edition messagebox acting strangely

ReneeC

 

I doubt "moments" is really accurate. Try nanosecond and then consider that the random class uses time as a seed when a seed is unspecified. Amessage box would give you a delay between instantiations and therefore a different internal seed.

If you have multiple instances of a class containing this random, consider changing it to a shared class so that there is only one and every time it's called, my guess is that since there is only one, it will not be a replication of the same thing. It will have one seed and be random from there instead of having multiple random classes probably having the same seed.






Re: Visual Basic Express Edition messagebox acting strangely

nobugz

The random number generator gets "seeded" by Environment.TickCount. That value changes only once every 15 milliseconds, give or take. If you call Random's constructor within those 15 milliseconds, you'll get the same sequence of numbers. The message box ensures that enough time passed.

The best solution is to move "Dim r As Random" outside the subroutine and make it a form member so it gets initialized only once.





Re: Visual Basic Express Edition messagebox acting strangely

Robert3234

Thanks so much guys... project.saved = true

-Robert