# Generating Random Numbers

• 06-16-2003
FromHolland
Generating Random Numbers
I am making this small program wich generates random numbers in order to make random sums. I checked the toturial to see how to generate random numbers using the system time, wich is never the same ofcourse. The problem is that I need to generare multiple random numbers at exactly the same time.

This is a piece of code I used. It will generate differend numbers IF the user does not press enter 3 times a second..

cout<<"Strike any key to generate first value";
cin.get();
time_t x;
time(&x);
srand((unsigned int)x);
sumvalueA = rand() % (lowX-highY/lowX)+lowX;

cout<<"Strike any key to generate second value";
cin.get();
int sumvalueB=0;
time_t y;
time(&y);
srand((unsigned int)y);
sumvalueB = rand() % (lowX-highY/lowX)+lowX;

cout<<"Strike any key to generate random sum";
cin.get();
int randomsum=0;
time_t z;
time(&z);
srand((unsigned int)z);
randomsum= rand() % (randsumX-randsumY/randsumX)+randsumX;

The outcome was 3 different values when the user waited a while before pressing a key, but faster pressing a key would make the same values. :(

Is there a different way of creating random numbers, without having to use the system time??
• 06-16-2003
You should only call the srand() function once at the start, not every time you want to call rand(). rand() will then produce a pseudo-random number each time. By restarting the generator with the same seed value, you will start the same pseudo-random sequence each time - as you have seen.

Code:

```time_t x; time(&x); srand((unsigned int)x); cout<<"Strike any key to generate first value"; cin.get(); sumvalueA = rand() % (lowX-highY/lowX)+lowX; cout<<"Strike any key to generate second value"; cin.get(); int sumvalueB=0; sumvalueB = rand() % (lowX-highY/lowX)+lowX; cout<<"Strike any key to generate random sum"; cin.get(); int randomsum=0; randomsum= rand() % (randsumX-randsumY/randsumX) +randsumX;```
• 06-16-2003
FromHolland
Isn't there any oter way of generating random numbers without having to use the system time?? I still have this frequecy in the random numbers.. Like this:

24
29
34
44
57

It's just NOT 100% random.. I can still see it uses the system clock..
• 06-16-2003
Hammer
Did you read this and this ?
• 06-16-2003
>>> It's just NOT 100% random

The second link from Hammer explains how the pseudo-random number sequence is generated and indicates some of the pitfalls.

Without special equipment, you will never get truly random numbers. Typical true random number generators of the likes used by electronic lottery systems use a radioactive isotope, older systems use noise diodes, but these tend to wear out after a period of use.
• 06-16-2003
FromHolland
Yes I KNOW! adrianxw told me before! And, you're wrong about the link thing, cuz there weren't any other links posted but yours when I posted my previous reply. And I cant use a link to some "use code tags" post anyway(yes I know I should use code tags, but I don't have alot of time) BTW, I didn't ignore any link, how could I ignore non-existing links?? Don't post nonsense plz.

But thx for the extra info anyway ;)
• 06-16-2003
UseCodeTags
"I know I should use code tags, but I don't have alot of time"

There are those who don't bother answering when you don't - you are reducing the number of potential helpers by not.