#include <cstdlib>
int number;
...
number =rand();//Whenever you need a random int
It's compiler-dependent. rand() will give you the posite range of int i.e [0,32767]Eddie said:does the random number generated in this way have a bound?
You need to do a Built-All after you chance your code (Ctrl-F11)Eddie said:and some how my rand() always give me 41 :smt102
//Program uses time function to seed random number generator
//and then generates random number
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
srand((unsigned)time(NULL));
int d=rand()%12;
cout<<d;
}
To generate a random number we use the rand() function. This will produce a result in the range 0 to RAND_MAX, where RAND_MAX is a constant defined by the implementation.
RAND_MAX is system dependent and is located within <stdlib.h> for C and <cstdlib> for C++. Determining its value is simple, just doEddie said:Could not quite make sense of the part on setting the RAND_MAX, so to translate that into actual code, how to generate a rand between say 1-100, which means need to set RAND_MAX???
cout <<RAND_MAX<<endl;
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
int j;
const int N = 100;
// Set initial seed
srand( (unsigned)time( NULL ) );
for (int i = 0; i < 5; i++) {
j = (int) N * rand() / (RAND_MAX + 1.0);
cout << j << endl;
}
return 0;
}
You can. How? Rewrite the C++ compiler.Eddie said:why is it not possible to overwrite or redefine constant RAND_MAX to other values?
srand doesnt seem that random to me (I've used this with implementation of normal distrib) lots of stuff on the net. Any good advice about generating the actual random number?
srand is a crappy random number generator. Try the Mersenne twister.