PDA

View Full Version : Random numbers for c++

Serac
Jan 28, 2006, 12:30 PM
Taking an example from the program above, I’d like to clarify a question I’ve been having.

c++

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
srand((unsigned)time(0));
int rn;
int lowest=1, highest=100;
int range=(highest-lowest)+1;
for(int index=0; index<20; index++){
rn=lowest+int(range*rand()/(RAND_MAX + 1.0));
}
if (rn<50)
{
cout<<”A random number is under 50”;
cout<<"("<<rn<<")";
}

So, with this program, it should supply a random number always different when run, right? Only 1, so I’d have to copy lines 8-14 again if I wanted a different random number?

Btw, I’m trying to get a “random” number from 1-100.

I’d appreciate any help. :)

Catfish_Man
Jan 28, 2006, 03:55 PM
srandomdev();
int rand = (random() * 10000) % 100;

This is just off the top of my head, but iirc it works fine. random() is better than rand().

gekko513
Jan 28, 2006, 05:39 PM
srand(time(0));
int a = rand() % 100 + 1;

This gives you a random number between 1 and 100 using srand and rand. Or you can probably use srandomdev as Catfish suggests, but I'm not familiar with that one.

Catfish, why do you multiply by 10000? If random() returns a long or int, that would make your expression evaluate to 0 every time, wouldn't it?

Catfish_Man
Jan 29, 2006, 03:41 PM
srand(time(0));
int a = rand() % 100 + 1;

This gives you a random number between 1 and 100 using srand and rand. Or you can probably use srandomdev as Catfish suggests, but I'm not familiar with that one.

Catfish, why do you multiply by 10000? If random() returns a long or int, that would make your expression evaluate to 0 every time, wouldn't it?

hmm, somehow I'd remembered random() as returning a double between 0 and 1. Like I said, that was just off the top of my head.

Thomas Harte
Jan 29, 2006, 04:53 PM
int a = rand() % 100 + 1;
The general advice when using rand() is that if you can't use all the bits, you should use the higher ones in preference to the lower ones. That's because many "random number" algorithms make a good job of covering the range described by RAND_MAX but a bad job of making every bit truly random, as a result of trying to maximise randomness without spending relatively large processing costs and the choice of random number algorithm is up to whoever implements rand().

If you could manage it without overflow, ((rand() * range) / RAND_MAX) would therefore always be better than rand()%range. On the Microsoft platform RAND_MAX is only (2^15)-1 or (2^16)-1 so you sometimes see that where people haven't really thought about what they're doing. On OS X RAND_MAX is 0x7fffffff - i.e. the highest positive number that can be stored in an signed int. So you do have to think about range.

If you're not that bothered about the degree of randomness then %range will probably do. If you can afford the costs, casting to floats and doing a multiply/divide op will supply the best variation. If you can write specifically for your range you can do something like:

((rand() >> 8)*range) / (RAND_MAX >> 8)

(for ranges up to 256, obviously). So for a random number in the inclusive range 0 to 100 you might try:

((rand() >> 7)*101) / (RAND_MAX >> 7)

Fender2112
Jan 29, 2006, 09:00 PM
Here's a little program the asks you to guess a number between 1 and 100. I think the concepts you want can be derived from this simple program.

#include <iostream>

using namespace std;

int main()

{
system("clear");

srandom(time(NULL)); // seed random number generator from internal clock
int fave;
fave = (random() % 100) + 1; // use random seed to generate number
int n;

//cout << fave << endl;

cout << "Guess a number from 1 to 100: ";
do
{
cin >> n;
if (n < fave)
cout << "Too low. Guess again: ";
else if (n > fave)
cout << "Too high. Guess again: ";
else
cout << fave << "\a\a\a ***** You must be psycic! ****" << endl;
} while (n != fave);

return 0;
}

Fender2112
Jan 29, 2006, 09:14 PM
So, with this program, it should supply a random number always different when run, right? Only 1, so I’d have to copy lines 8-14 again if I wanted a different random number?

Btw, I’m trying to get a “random” number from 1-100.

I’d appreciate any help. :)

You can write a function and call it each time you need a random number

void GetRandomNumber() // function prototype

void GetRandomNumber() // function definition
{
srandom(time(NULL));
return (random() % 100)+1;
}

x = GetRandomNumber(); // call to function

Fender2112
Jan 29, 2006, 09:14 PM
delete double post :D

mrichmon
Jan 30, 2006, 12:31 AM
You can write a function and call it each time you need a random number

void GetRandomNumber() // function prototype

void GetRandomNumber() // function definition
{
srandom(time(NULL));
return (random() % 100)+1;
}

x = GetRandomNumber(); // call to function

Since you are reseeding the random number generator each time you invoke this method then the generated numbers can be predicted. (Also, the function needs to be declared as returning an integer.)

What you really want to do in this case is:

int getRandomNumber();

short randomSeeded = 0;

main() {
printf(" %d\n", getRandomNumber());
printf(" %d\n", getRandomNumber());
printf(" %d\n", getRandomNumber());
}

int getRandomNumber() {
if(randomSeeded == 0) {
srandom(time(NULL));
randomSeeded = 1;
}

return (random()%100) + 1;
}