views:

857

answers:

4

main .cpp

#include        "stdafx.h"
#include        "random_generator.h"


        int
main ( int argc, char *argv[] )
{
        cout.setf(ios::fixed);
        base_generator_type base_generator;
        int max = pow(10, 2);
        distribution_type dist(1, max);

        boost::variate_generator<base_generator_type&,
distribution_type > uni(base_generator, dist);
        for ( int i=0; i<10; i++ ) {
                //cout << random_number(2) << endl;
                cout << uni() << endl;
        }

        return EXIT_SUCCESS;

}                               /* ----------  end of function main  ---------- */

random_gemerator.h

#include        "stdafx.h"

#include        <boost/random.hpp>
#include        <boost/generator_iterator.hpp>

typedef boost::mt19937 base_generator_type;
typedef boost::lagged_fibonacci19937 fibo_generator_type;
typedef boost::uniform_int<> distribution_type;
typedef boost::variate_generator<fibo_generator_type&,
distribution_type> gen_type;

        int
random_number ( int bits )
{
        fibo_generator_type fibo_generator;
        int max = pow(10, bits);
        distribution_type dist(1, max);

        gen_type uni(fibo_generator, dist);
        return uni();

}               /* -----  end of function random_number  ----- */

stdafx.h

 #include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

every time I run it, it all generate the same number sequence

like 77, 33,5, 22 , ...

how to use boost:random correctly?

+4  A: 

You need to seed your random number generator so it doesn't start from the same place each time.

Depending on what you are doing with the numbers, you may need to put some thought into how you choose your seed value. If you need high quality randomness (if you are generating cryptographic keys and want them fairly secure), you will need a good seed value. If this were Posix, I would suggest /dev/random - but you look to be using Windows so I'm not sure what a good seed source would be.

But if you don't mind a predictable seed (for games, simulations, etc.), a quick and dirty seed is the current timestamp returned by time().

R Samuel Klatchko
Ya right. If the seed is the same, the generator will start with the same random number everytime
A9S6
that is it. but maybe have a little problem, like the following:it seems sound get_seed(); for (;;) {cout << generate_random() << endl; } // is ok it genereate the same random number int get_random() {get_seed();return generate_random();} for (;;) {cout << get_random() <<endl;} // output the same random number yet
mono
generate a seed every time the function is called. vs. use the same seed every time which is the same seed?why I get the same random number sequence in the former way
mono
+10  A: 

if you want the sequence of random numbers to change every time you run your program, you need to change the random seed by initializing it with the current time for instance

you will find an example there, excerpt:

/*
 * Change seed to something else.
 *
 * Caveat: std::time(0) is not a very good truly-random seed.  When
 * called in rapid succession, it could return the same values, and
 * thus the same random number sequences could ensue.  If not the same
 * values are returned, the values differ only slightly in the
 * lowest bits.  A linear congruential generator with a small factor
 * wrapped in a uniform_smallint (see experiment) will produce the same
 * values for the first few iterations.   This is because uniform_smallint
 * takes only the highest bits of the generator, and the generator itself
 * needs a few iterations to spread the initial entropy from the lowest bits
 * to the whole state.
 */
generator.seed(static_cast<unsigned int>(std::time(0)));
Gregory Pakosz
that is it. but maybe have a little problem, like the following:it seems sound get_seed(); for (;;) {cout << generate_random() << endl; } // is ok it genereate the same random number int get_random() {get_seed();return generate_random();} for (;;) {cout << get_random() <<endl;} // output the same random number yet
mono
A: 

that is it. but maybe have a little problem, like the following:

it seems sound

get_seed(); for (;;) {cout << generate_random() << endl; } // is ok

it genereate the same random number

int get_random() {get_seed();return generate_random();} for (;;) {cout << get_random() <<endl;}  // output the same random number yet
mono
Where did you set the random Seed? I must admit I am not familiar with the API you are using, but it seems to me you are not '''setting''' the seed
Shaihi
get_seed() is generator.seed(static_cast<unsigned int>(std::time(0)));
mono
A: 

If you are running on a 'nix system, you could always try something like this;

int getSeed()
{
    ifstream rand("/dev/urandom");
    char tmp[sizeof(int)];
    rand.read(tmp,sizeof(int));
    rand.close();
    int* number = reinterpret_cast<int*>(tmp);
    return (*number);
}

I'm guessing seeding the random number generator this way is faster than simply reading the /dev/urandorm (or /dev/random) for all your random number needs.

Chris Huang-Leaver