Sign in to follow this  

[.net] Random Numbers ?

This topic is 4834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What would be the best way to generate random numbers for games. At the moment I use the following to seed the generator. Can anyone think of another way Random mRand = new Random(unchecked((int)DateTime.Now.Ticks));

Share this post


Link to post
Share on other sites
That will work fine for games. Random will provide a nice even distribution of values, especially if you use it's ranged versions instead of mocking up your own.

For more complex applications however, you might want to invest in another random number algorithm. From examining the source code of System.Random, it appears to use a 56 state congruency state machine.

Share this post


Link to post
Share on other sites
Just thought I'd mention you don't have to include the time parameter. The following:

Random mRand = new Random();

works just as well. If there's no parameter, the constructor basically calls new Random(Environment.TickCount), so both statements below are equivalent:

Random mRand = new Random(); //This is the same...
Random mRand = new Random(Environment.TickCount); //...as this.

Share this post


Link to post
Share on other sites
Hmmm. When I made my particle system, the snow came down as if emitted from columns moving along when in fact it was
Random.NextDouble * emitterWidth

Share this post


Link to post
Share on other sites
The dotnet framework has another random generator that should produce better random data. Check out System.Security.Cryptography.RNGCryptoServiceProvider -- though it is probably overkill. Some modern hardware configuration have physical devices to generate random information (I believe that both modern intel and AMD processors have a built in RNG) though how to access it from dotnet I do not know.

Share this post


Link to post
Share on other sites

This topic is 4834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this