View Full Version : How to generate a random number in C++?
Eddie
09-03-2006, 02:04 AM
say if i have an integer called number, how to get a random value for it when every time i access it?
int number = ???? ;
#include <cstdlib>
int number;
...
number =rand();//Whenever you need a random int
Eddie
09-03-2006, 02:25 AM
does the random number generated in this way have a bound?
... ...
and some how my rand() always give me 41 :smt102
does the random number generated in this way have a bound?
It's compiler-dependent. rand() will give you the posite range of int i.e [0,32767]
and some how my rand() always give me 41 :smt102 You need to do a Built-All after you chance your code (Ctrl-F11)
Also, to prevent the same number generated,use srand()
//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;
}
More about random generators in C++ here (http://www.daniweb.com/techtalkforums/thread1769.html)
In general, you can google for much of the info you are after. It's more time effective.
Eddie
09-03-2006, 02:52 AM
I read abou the part on this 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.
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???
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???
RAND_MAX is system dependent and is located within <stdlib.h> for C and <cstdlib> for C++. Determining its value is simple, just do
cout <<RAND_MAX<<endl;
Most likely, you will see 32767 as output. So, you can't change RAND_MAX.
You have the same 41 every time you run rand() because it uses the same seed (srand() initiates that seed). Put it in a for loop to get different numbers.
To generate 1-100, use this
#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;
}
Eddie
09-03-2006, 03:35 AM
why is it not possible to overwrite or redefine constant RAND_MAX to other values?
why is it not possible to overwrite or redefine constant RAND_MAX to other values?You can. How? Rewrite the C++ compiler.
It's not how the compiler defines some system global CONST, it's how you implement your code to achieve the desired goal.
RussianMike
11-25-2006, 09:04 PM
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?
vBulletin® v3.8.0 Beta 3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.