Public Group

# Question about the build in Rand() function.

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

## Recommended Posts

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 on other sites

Servant Of The Lord, you just explained why how computers generate random numbers (it had been a mystery to me :))!

Cheers :)!

##### Share on other sites

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 on other sites

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 on other sites
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.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 9
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633679
• Total Posts
3013300
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!