Thread: Is rand() not truly random after all?

1. Is rand() not truly random after all?

Please bear with me as I am new to the ways of the C++ programming and the art of the compiled language. I just finished a project for the MS class and I ran into something that does not make sense to me. Maybe someone here can shed some light on the matter...

When using:
Code:
int listA = rand();
I get an array consisting of 10 numbers randomly generated. However when running the code multiple times I get the same 10 numbers every time.

When using:
Code:
int listA = srand(time(NULL));
I get a true random array that changes each time I execute the code.

Why is it that rand() does not generate a different random result each time it is executed? In other languages I've used in the past (MEL, AutoLISP) the rand function will generate a truly random number every time it is executed. In fact the odds of getting two exact numbers from rand() is not one I would wager money on in Vegas.  Reply With Quote

2. Re: Is rand() not truly random after all?

Originally posted by DezFX
Please bear with me as I am new to the ways of the C++ programming and the art of the compiled language. I just finished a project for the MS class and I ran into something that does not make sense to me. Maybe someone here can shed some light on the matter...

When using:
Code:
int listA = rand();
I get an array consisting of 10 numbers randomly generated. However when running the code multiple times I get the same 10 numbers every time.

When using:
Code:
int listA = srand(time(NULL));
I get a true random array that changes each time I execute the code.

Why is it that rand() does not generate a different random result each time it is executed? In other languages I've used in the past (MEL, AutoLISP) the rand function will generate a truly random number every time it is executed. In fact the odds of getting two exact numbers from rand() is not one I would wager money on in Vegas.
srand(time(NULL)) is called seeding the random. While creating, editing, and debugging, its best to stay away from it. Only when your happy your program works you should include it. And you only need to include it before you ever use rand() and you will get *random* numbers p.s. if you want to limit rand() type
PHP Code:
(rand()%MAX_VALUE)+
where MAX_VALUE is the the biggest value you want, and as c++ is 0 based, the +1 aligns it, if thats what you want Last edited by craigw1701; 12-01-2004 at 03:22 PM.  Reply With Quote

3. Tidbit of computing theory.

Random numbers do not exist in computers :-).

The solution given by craigw is correct. The sequence of random numbers is derived entirely from a single seed and if you set the seed to a timestamp then there is a high chance your not going to use the same seed twice.

So rand() is actually simulating randomness.  Reply With Quote

4. Oh, sorry about that... My code was a mistake in that I use:
Code:
srand(time(NULL));
int listA = rand();
This will give me random numbers. I understand what seeding is and the use of the modulus to build a range for the numbers, but the need for random seeding is what I was having a hard time grasping.

So, what I get from the explanations you guys provided is that you have a number table in the machine code. The seeding places a noise-like pattern in where the random values are derriven from in that table. Thus returning different numbers each time because the array is built from a different starting point each time rand() is called. Is that correct?  Reply With Quote

5. if you want a better random number generator search for the mersenne twister
its better than rand()

or go to code project and search for it also(mersenne twister)  Reply With Quote

6. think of the rand()-function as a mathematical function that calculates from your input a number within a specific range.

like any other mathematical function it will give you the same output for same input values every time! so if you start rand() without initializing it, it will use the default value (which is 1) as an input and thus give you the exact same sequence of numbers every time you run your program.
to prevent this bug you give the function the current time as starting value, which will cause it to give you kind of random numbers.

that's how the function calculates the first random number, but what if i'm in need for a whole bunch of random stuff?
the rand-function itself takes care of this: because the randomizer was initialized by the timer, you can assume the first number as likely random. now the function keeps this first output value in mind and uses it as an input value when you call the rand() function again.
the last output from rand() will always be the input when you call the funtion again! but because your first output was random, the following outputs will be random as well!

a common beginner's mistake when using rand() frequently is that they call srand() every time they call rand(). This will screw up your randomizer completely. The computer probably executes the code faster than the time-value changes, so you will get the same random number all over the place!

that's pretty much everything about the rand()-function, if you call the srand() only once (no more, no less!) it should work fine for you!  Reply With Quote

7. Most random number generators work like this: You have a seed called x. rand() has a "random" function that really isn't random, it just looks like it. The first time you call it, it will return f(x). The second time, it'll return f(f(x)). The third time, it will return f(f(f(x))). Basically, each time you call the function, it calculates the next number in the sequence and gives it to you. So you need a seed. If you don't give one, it will just use the default one. Usually the time is used, because it's different every time. (Unless you run your program more than once in a second.) I'm pretty sure MEL and AutoLisp do this for you quietly, though C isn't so nice.

Incidentally, I had a friend that had a randomistic algorithm that was supposed to run over and over... for 5 seconds. It wasn't working as well as was expected. In the end, he discovered that he had srand(time(0)) in his main loop. So the random algorithm was essentially only running 5 times!
Last edited by halma; 12-02-2004 at 06:23 AM.  Reply With Quote

8. OK, I think I got it now. Thanks for the help, guys.

This is what I have:
Code:
#include <iostream>

#include <time.h>

using namespace std;

main()
{
int listSize = 20;
int listA = {0};
int listB = {0};
int i = 0;

srand(time(NULL));

for (i = 0; i < listSize; i++)
{
listA[i] = rand() % 1000 + 1;
cout << "Number " << i + 1 << " = " << listA[i] << endl;
}

for (i = 0; i < listSize; i++)
{
listB[i] = rand() % 1000 + 1;
cout << "Number " << i + 1 << " = " << listB[i] << endl;
}

// more code here //

}
Is this how you were suggesting it? Where calling the srand() command will work for both rand() functions and only be called once in the main proceedure because it was called outside of any loop?  Reply With Quote

9. Originally posted by DezFX
calling the srand() command will work for both rand() functions and only be called once in the main proceedure because it was called outside of any loop?
Not just both times you're calling the rand() function -- all 2000 times! It's called each time that line comes up in either loop.  Reply With Quote

10. you forgot to include the stdlib.h which holds the rand() function   Reply With Quote Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•