RAND vs Mersenne Twister

Hello, I am trying to make a relatively unbiased random number generator that generates a random number between 1 and 9. The following code is a comparison between a Mersenne Twister algorithim and and a RAND algorithm. The MT keeps outputting a '6', whereas the RAND outputs more random numbers, although I suspect it's more biased than the MT here (if MT worked). My hunch is that this is a seeding problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <random>
#include <time.h>
using namespace std;

int main()
{
    mt19937(time(0));

    srand(time(0));

    cout << "A random number will be generated between 1 and 9" << endl;


    std::random_device rd; // obtain a random number from hardware
    std::mt19937 eng(rd()); // seed the generator
    std::uniform_int_distribution<> distr(1, 9); // define the range

    cout << distr(eng) << endl;

    int n = 1 + (rand() % ( 9 - 1 + 1 ));
    cout << n << endl;
}
Does rd() provide a different value each time you call it?

std::cout << "Value from rd: " << rd() << std::endl;
What value do you get for rd.entropy() ?

If it is zero, you would be better off ignoring std::random_device
and replace line 16 with std::mt19937 eng(time(0)); // seed the generator

http://www.cplusplus.com/reference/random/random_device/entropy/
Last edited on
Topic archived. No new replies allowed.