1

I have been trying to use the c++ 11 mersenne_twister_engine class( http://www.cplusplus.com/reference/random/mersenne_twister_engine/) to generate numbers in the interval [0,1] however I am continually getting 0 on every number.

Here is my code snippet

unsigned seed1 = std::chrono::system_clock::now().time_since_epoch().count();   

std::mt19937 generator(seed1); 
for (int i=0; i < WIDTH * HEIGHT; i++) //GENERATES THE MATRIX OF CONNECTIVITY
{

    double random = generator()/generator.max();
    if ( random  > VERT_CONNECTIVITY )
    vert_con[i] = 0;
    else vert_con[i] = 1;


}

generator() and generator.max() seem to be working... It is just random which gives me 0!

Thanks!

5
  • 1
    An integer divided by an integer is an integer.
    – chris
    Commented Dec 11, 2013 at 18:25
  • 1
    Why not use uniform_int_distribution? Commented Dec 11, 2013 at 18:25
  • if int a < int b (a/b=>0) (a%b=>a)
    – Jekyll
    Commented Dec 11, 2013 at 18:28
  • 5
    bernoulli_distribution is probably the better choice, given the binary result
    – Cubbi
    Commented Dec 11, 2013 at 18:29
  • 2
    Also keep in mind you can use std::random_device instead of a time seed.
    – chris
    Commented Dec 11, 2013 at 18:31

4 Answers 4

7

Using one of the distributions provided in the random header probably makes more sense. As Cubbi points out std::bernoulli_distribution looks like it fits your problem well. It will generate true or false according to the distribution parameter you pass in:

#include <iostream>
#include <random>
#include <vector>
#include <algorithm>

const int WIDTH = 5 ;
const int HEIGHT = 5 ;
const double VERT_CONNECTIVITY = 0.25 ;

int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());

    // give "true" 1/4 of the time
    // give "false" 3/4 of the time
    std::bernoulli_distribution d(VERT_CONNECTIVITY);

    std::vector<int> vert_con( WIDTH * HEIGHT ) ;

    std::generate( std::begin(vert_con), std::end( vert_con ), [&] () { return d(gen) ; } ) ;

    for (int i=0; i < WIDTH * HEIGHT; i++) 
    {
        std::cout << vert_con[i] << " " ;
    }
    std::cout << std::endl ;

    return 0 ;
}
1
  • OP is probably looking for std::generate(std::begin(vert_con), std::end(vert_con), [&]{ return d(gen); });
    – Casey
    Commented Dec 11, 2013 at 18:44
3

Perhaps you are looking for generate_canonical<double, bits>(gen)? This will generate a decimal value uniformly distributed in the interval [0,1) with bits bits of randomness (53 being the standard maximum available for a double).

1

In C++, / on integer numbers results in an integer number. For example 11 / 2 results in 5, not 5.5. In the same way a / b is always zero if a < b.

Your problem is here:

generator()/generator.max()

generator() and generator.max() both return integers and of course, generator.max() >= generator(), so the result is zero (unless you are very lucky to get the max number in which case the result would be one).

To fix this, you can simply cast it:

(double)generator()/generator.max()
0
1

The mersenne_twister_engine returns results of type UIntType. If you want the result to be a double, you need to cast it to a double.

double random = (double)generator()/generator.max();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.