Random Integer Problem C++

BigJimbo

Golden Member
Aug 4, 2002
1,193
0
0
Its doing the same random numbers....ie

1x7
4x0

ect.... everytime. Yes they are random but they are in the same order.

maybe a new set of eyes can spot it
 

EpsiIon

Platinum Member
Nov 26, 2000
2,351
1
0
You need to seed the rand function with srand(). A common seed is:

time(NULL)

If you're working on some UNIX variant, I'd recommand executing the following commands:

man rand
man 2 time

That should give you all the information you need (and more).
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
you need to "seed" the random generator since it's only pseudo-random

the most common way is to use the time before you get the random numbers

eg srand(time(NULL));

don't call it too often. just once for each set of random numbers should be enough
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
On unix I like to do srand(time(NULL) + getpid()), so that if it's a quick little program and you run it multiple times in a second, it will still act random.

gettimeofday() would do the trick as well, but has a bit more of a syntax burden.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
The random generator by default is seeded to a constant reference.

As others above have said, you need to seed it with a continually changing variable.