PDA

Click to See Complete Forum and Search --> : Random number


ChimpFace9000
Mar 5th, 2001, 12:50 PM
Does windows have a random number function?

parksie
Mar 5th, 2001, 01:08 PM
Don't think so, but the standard library does:

#include <iostream>
#include <cstdlib>

using namespace std;

void main() {
cout << rand() << endl;
}

rand returns a number between 0 and RAND_MAX.

SteveCRM
Mar 5th, 2001, 02:06 PM
or this


#include <iostream.h>
#include <stdlib.h>
#include <time.h>

int main()
{
srand(time(NULL));
cout<<1*rand()%6<<endl;
return 0;
}

Mar 5th, 2001, 02:44 PM
How do i get one between 0 and 99?

SteveCRM
Mar 5th, 2001, 03:08 PM
on mine you would do 0*rand()%99

parksies I think you would do this


#include <iostream>
#include <cstdlib>

using namespace std;

void main() {
RAND_MAX = 99
cout << rand() << endl;
}

parksie
Mar 5th, 2001, 03:21 PM
RAND_MAX is already defined -- use:

float myrand(float lower, float higher) {
return (((float)rand() / (float)RAND_MAX) * (higher-lower)) + lower);
}

Mar 5th, 2001, 03:28 PM
Thanks, but both those return the same sequence of numbers each time.

parksie
Mar 5th, 2001, 03:39 PM
Seed the random number using srand at the start of your program, using the time as stated earlier.

Mar 5th, 2001, 04:28 PM
It STILL gives the same sequence of numbers.