creating a random number within a desired range in visual c++...how?

piski

Senior member
Jan 21, 2002
312
0
0
I need to generate a random number between 1 and 13. Its a random card dealing program. i tried to use rand() but it need the library stdlib and when i included that, the compiler said that it couldnt find it. what should i do?
 

piski

Senior member
Jan 21, 2002
312
0
0
ok. i guess for some reason stdlib.h worked. however. when i say
cout << rand() it keeps showing 41. How can i give it parameters like between 1 and 13? I believe its RAND_MAX....
thanks
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
stdlb is a C standard library not the C++ STL

In the friendly Books Online:

"The rand function returns a pseudorandom integer in the range 0 to RAND_MAX. Use the srand function to seed the pseudorandom-number generator before calling rand."

So:
1. seeding the generator -- Books Online sample code shows you how to do this using the time function

2. limiting values to a range. Hmmm, what does % do? (Hint 2: then add +1)
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Originally posted by: piski
what does seed mean? initialize?
Yes. Pseudo random number generators take the initial value and use it to create all following values.

In CS a seed = starting / initial value or state, since future values or states "grow" out of it.