Random Numbers? What to use?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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));
 
You can also use System.Security.Cryptography.RNGCryptoServiceProvider with
GetBytes() and GetNonZeroBytes() methods. It's a bit slower than Random, but
every byte is computed with a seed dependent on lots of environment
conditions so it is almost random (that is why it is used in cryptographic
functions). Random bytes are linear beginning with the value computed with
constructor seed.
Hope this helps.
Wojtek
 
Glenn Wilson said:
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));

If you don't need cryptographic quality, this is usually sufficient:

Random mRand = new Random();

According to the docs, it initializes the Random object with a
time-dependent value.

Niki
 
According to the docs, it initializes the Random object with a
time-dependent value.

This is true. The Random class is internally instantiated using the
DateTime.Now.Ticks value. However, if you're up to generating large amounts
of random numbers, I would probably look into reusing a Random class
instance (or having an instance stored somewhere, then invoked), instead of
instantiating each time you need a random number (as the ticks property
isn't 100% realtime).
 
This is true. The Random class is internally instantiated using the
DateTime.Now.Ticks value. However, if you're up to generating large
amounts
of random numbers, I would probably look into reusing a Random class
instance (or having an instance stored somewhere, then invoked), instead
of
instantiating each time you need a random number (as the ticks property
isn't 100% realtime).

Never mind that, if you re-instantiate the standard Random class not only
you pay a certain performance quality, but also, none of the statistical
properties of the generator are guaranteed.
The correct way of using any random number generator is to initialise it and
then use its "Next()" method to extract subsequent random numbers.
 
Never mind that, if you re-instantiate the standard Random class not only
you pay a certain performance quality, but also, none of the statistical
properties of the generator are guaranteed.
The correct way of using any random number generator is to initialise it and
then use its "Next()" method to extract subsequent random numbers.

Naturally, but he asked and I wanted to let him know of this behaviour. Any
problems with that? ;-)
 
Anders Borum said:
Naturally, but he asked and I wanted to let him know of this behaviour.
Any
problems with that? ;-)

I somehow assumed the OP already knew that. He only asked how to seed the
RNG. I guessed someone who knew what a "seed" is should know how a PRNG
works (basically).

Niki
 
Thanks for the help, and Yes I do know about the Next call, I was just
asking about the prefered method of seeding as I will need fast performance
and complete random numbers. I have seen systems in the past that when
generating random numbers depending on how fast they are called can produce
the same series of numbers.
 
Back
Top