Sign in to follow this  
JackBid

Question about the build in Rand() function.

Recommended Posts

JackBid    453

So I think in just about every programming language there is some sort of build in Rand() function which chooses a random number between 0-1 which can then be multiplied to achieve a random point between two numbers.

 

My question is; in simple terms, does anyone know how the rand() function works? I was just thinking about it and don't really understand how it is possible to do. Theoretically what is going on inside that function?

 

P.S. I am a beginner and if anything said above was incorrect please forgive me. 

Share this post


Link to post
Share on other sites
Bacterius    13165

The most common and simplest to implement is probably the LCG (linear congruential generator) which is pretty much just:

 

next value = (a * old value + b) mod m

 

For some initial value. With careful selection of the constants a, b, m, the generator will have maximum period equal to m values before repeating (in general, m is 2^32 or 2^64). But it kind of sucks in practice. If you plot the results they don't look random at all but display a lattice structure, even though individual values look random enough.

 

It is possible to implement true random number generation in computers, but it cannot be done completely in software due to determinism. For instance, under Linux, reading /dev/random will give you truly random bits, which are usually obtained from various unpredictable sources such as keystrokes, mouse movements, disk access latency, thermal noise, and can even use stuff produced by dedicated hardware (for instance, there are relatively cheap hardware random number generators based on quantum physics).

 

But for almost all applications a pseudorandom number generator will do fine. If you use C++11 you can check out the extra random number generators implemented in the standard library, that way you don't have to rely on rand() and can use any available generator without even having to code it yourself. Otherwise implementations exist in other languages, I am sure.

Share this post


Link to post
Share on other sites
jbadams    25677

Note that when using pseudo-random numbers you're almost always able to provide a "seed" in some way; this is usually something you'll want to do once at the start of your program.

 

By providing a different seed you get a different sequence of random numbers, whilst providing the same one will give you the same sequence each time.  This can be useful if you're using your "random" numbers to generate maps, spawn bad guys, etc., because you can re-produce the same results each time by using the same seed values.

Share this post


Link to post
Share on other sites
iMalc    2466
Most of them are really simple. Here's one from a common compiler:
long holdrand;

void __cdecl srand (unsigned int seed)
{
    holdrand = (long)seed;
}

int __cdecl rand (void)
{
    return(((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);
}
As you can see, it's really really simple.
No rand() in C doesn't return a float from 0 to 1. It returns an int.

Here is an example of a much better PRNG: http://en.wikipedia.org/wiki/Xorshift
Still quite simple, but far far better.

Share this post


Link to post
Share on other sites

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