Random Number Generators In Programming

If you have fiddled around enough with C/C++/Objective-C, you must have noticed that if you use rand() function on its own, it gives the exact same numbers every time. Now how is that possible? Isn’t rand() function supposed to generated completely random numbers in a given range? The reason for this has something to do with the seed value. If you use the same seed value every time, then you will get the same numbers.  

Wait a minute, what is a seed value?

Whenever you create a random number generator formula, you have to have a starting point for the formula to grow from. This point is known as the seed value. In essence, it determines the path the formula will follow to produce its results. For example, if you were to use the number 3 as a seed value for your formula, and then you ran it again with a seed value of 7, the formula would produce radically different results. However, if you were to run it once more with a seed value of 3, the results would be the same as your first attempt. Without a seed value, your formula is simply a set of mathematical equations that lead nowhere.

What happens if I don’t specify a seed value?

If you just use rand() function without explicitly specifying the seed value, you will get the same numbers every time. If no seed value is provided, the rand() function is automatically seeded with a value of 1. Since it is always seeded with the same number, it will always produce the same sequence of numbers. To get it to produce a different sequence each time it runs, you need to use a different seed each time.

Choosing a seed value is not as straightforward as picking a number like 3 or 7. Real-world seeds are actually very complex. For a random number generator formula to produce truly random results, the initial seed value for the formula must itself be a random number. Many programs use a number with a constantly changing value to achieve this, such as the current date and time converted to a simple integer value.

How do we do it in programming?

In C/C++/Objective-C, you can use srandom() to set the seed value and then use random() function to generate a random value. The function “srandom()” is short for “seed random”. On a side note, rand() and srand() are also valid, but they have been deprecated. We can use the current time to seed the random number generator in the following way (in C++):

#include <stdlib.h>
#include <time.h>
...
srandom(time(NULL));
int randomNumber = random() % 100;
cout << randomNumber << " is actually random now!" << endl;

————————————————————————————————-

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s