Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random numbers 1

Status
Not open for further replies.

jubble

Programmer
Mar 6, 2002
207
GB
What's the best seed for generating random numbers in the Random class?

I heard making it time-based ensures absolute randomness but should this be from the 'Now' function or is the system.environment.tickcount value good enough?

 
As long as it is random, it doesn't matter where it came from.

__________________________________________
Try forum1391 for lively discussions
 
but it won't be random if the same seed is used.

try it yourself...get a bunch of random numbers with the same seed twice and you'll find that you get random numbers but they are exactly the same random numbers that you got last time.

that's what the seed is for.

it's similar to having to declare Randomize in VB6 to prevent this from happening.

 
Either of the values you mentioned would be fine, because they change. Theoreticly I guess getting the now value would be better as it could be possible to hit the same tick on a susequent execution of the app, although the likely hood is pretty low.

-Rick

----------------------
 
According to .Net documentation, the default seed is from the system clock, so you don't need to seed it--it will do it by itself. But, if it runs to fast to get different values from the clock, there is a suggestion on MSDN to take care of that:


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top