• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

How to generate a random number in C++?

Joined
5/17/06
Messages
99
Points
26
say if i have an integer called number, how to get a random value for it when every time i access it?
int number = ???? ;
 
does the random number generated in this way have a bound?
... ...
and some how my rand() always give me 41 :smt102
 
Eddie said:
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]
Eddie said:
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()
C++:
//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
In general, you can google for much of the info you are after. It's more time effective.
 
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???
 
Eddie 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???
RAND_MAX is system dependent and is located within <stdlib.h> for C and <cstdlib> for C++. Determining its value is simple, just do
C++:
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
C++:
#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;
}
 
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?
 
Of course, you need context for crappy to have any meaning. :)

For a large number of purposes, rand() is fine.

If you want to generate RNs that are suitable for crypto, you couldn't do much better than reading bits off of /dev/random (preferred) and /dev/urandom which get their randomness from environmental noise. That's why when you generate a GPG key, the program asks you to move the mouse around and type at the keyboard. It's storing "entropy" and converting it to RNs. I would say these device files can truly be called "RNGs" in every sense of the word (acronym). However, you'll definitely run out of entropy if you try to use them with MC. See Google for more info on these two device files. Of course, they're not available on Windows (to my knowledge), so you'll have to fall back to something else, like the Mersenne twister.

Note that there are places on the net that sell random numbers.

For Monte Carlo, I've heard (but haven't used myself) that sobol sequences are the best way to go. This would be the case where best random numbers aren't random at all.
 
You should avoid C rand() at all costs; RAND_MAX is a 16-bit integer and after a million RNGs all the values are the same. Even VB is better because it's max is 32 bits.

As mentioned, MT is a good one. Here is a great place to download it Boost Random Number Library


//
BTW srand() is NOT a generator; it sets the seed for the generator rand()
 
Back
Top