C++ rand()%

LeetViet

Platinum Member
Mar 6, 2003
2,411
0
76
I have a switch and in a case I want it to randomly choose one, ie

case 1:
{
card=1;
card=2;
card=3;
(insert randomize)
}
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
look up the ANSI C functions rand() and srand() to "seed" the random number generator. the % mod operator can trim the value to any range you want, e.g.

pick = 1 + (rand() % 10) ; // 1 - 10

pick = rand() % 50 ; // 0-49

people often use the clock to seed the RNG, for example:

srand( (unsigned)time( NULL ) ) ;

you only need to do this once, at the start of your program or before the first call to rand() .
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
I like to use a combination of time(NULL) and getpid(), so that multiple executions within the same second don't result in identical "random" numbers.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: DaveSimmons
look up the ANSI C functions rand() and srand() to "seed" the random number generator. the % mod operator can trim the value to any range you want, e.g.

pick = 1 + (rand() % 10) ; // 1 - 10

pick = rand() % 50 ; // 0-49

This is generally considered to be bad practice, as it only uses the low order bits of the random number, which may not be very random for some of the crappier algorithms.

You should use something like:

pick = int(50*rand()/RAND_MAX);

(got burned by this once)

people often use the clock to seed the RNG, for example:

srand( (unsigned)time( NULL ) ) ;

you only need to do this once, at the start of your program or before the first call to rand() .

 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Originally posted by: BingBongWongFooey
I like to use a combination of time(NULL) and getpid(), so that multiple executions within the same second don't result in identical "random" numbers.
Good idea for short-lived processes!

/me steals your idea :)

 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Originally posted by: Armitage
This is generally considered to be bad practice, as it only uses the low order bits of the random number, which may not be very random for some of the crappier algorithms.

You should use something like:

pick = 50*rand()/RAND_MAX;

(got burned by this once)
Agreed if you need highly random numbers, but this looked like an Intro to C++ programming assigment so I wanted to keep it simple.

 

LeetViet

Platinum Member
Mar 6, 2003
2,411
0
76
So
card = 50*rand()/3; ?

Edit: Will this prevent it from choosing numbers that I don't want it to?
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Originally posted by: LeetViet
So
card = 50*rand()/3; ?
the RAND_MAX Armitage mentioned is a constant equal to the largest value that rand() can possibly return. So if you do the math,

int y = (x * rand() ) / RAND_MAX

will set y to a value between 0 and x. Either that or garbage after an integer overflow :)

edit: actually between 0 and x not the (x-1) I said originally:
"The constant RAND_MAX is the maximum value that can be returned by the rand function. RAND_MAX is defined as the value 0x7fff."

If this were an environment where you cared about the best possible randomness, you could use the above approach but cast the intermediate values to 64-bit long long ints to prevent overflow.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: DaveSimmons
Originally posted by: LeetViet
So
card = 50*rand()/3; ?
the RAND_MAX Armitage mentioned is a constant equal to the largest value that rand() can possibly return. So if you do the math,

int y = (x * rand() ) / RAND_MAX

will set y to a value between 0 and x. Either that or garbage after an integer overflow :)

edit: actually between 0 and x not the (x-1) I said originally:
"The constant RAND_MAX is the maximum value that can be returned by the rand function. RAND_MAX is defined as the value 0x7fff."

If this were an environment where you cared about the best possible randomness, you could use the above approach but cast the intermediate values to 64-bit long long ints to prevent overflow.

Yea, actually you should probably just make x in your equation above a floating point number, or you will definitely get overflow. RAND_MAX on my X86 linux box is 2^31 ... ie. the maximum size of a signed int. And long int is the same size.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: LeetViet
How can I randomize between X numbers of specific numbers?

Well, the discussion above gives you random numbers between 0 and some other number. Getting random numbers between 2 nonzero numbers is left as an exercise for the student.
 

LeetViet

Platinum Member
Mar 6, 2003
2,411
0
76
Originally posted by: Armitage
Originally posted by: LeetViet
How can I randomize between X numbers of specific numbers?

Well, the discussion above gives you random numbers between 0 and some other number. Getting random numbers between 2 nonzero numbers is left as an exercise for the student.

This isn't for a school project.:confused:

What do you mean by 2 nonzero numbers?
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: LeetViet
Originally posted by: Armitage
Originally posted by: LeetViet
How can I randomize between X numbers of specific numbers?

Well, the discussion above gives you random numbers between 0 and some other number. Getting random numbers between 2 nonzero numbers is left as an exercise for the student.

This isn't for a school project.:confused:

What do you mean by 2 nonzero numbers?

Eh? It's just a figure of speech.
What I'm saying is that we've outlined the special case of (0, x). The extension of the algorithm to the general case of (y, x) is trivial, and "left as an exercise for the student" (or reader).