Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::default_random_engine generate values between 0.0 and 1.0

Tags:

c++

std

c++11

I want to be able to generate random values between 0.0 and 1.0

I've tried to use

std::default_random_engine generator;
std::uniform_real_distribution<float> distribution(0.0, 1.0);

float myrand = distribution(generator);

Generating random value in a loop gives me always these values:

0.000022

0.085032

0.601353

0.891611

0.967956

0.189690

0.514976

0.398008

0.262906

0.743512

0.089548

What can I do to really get random values? Doesn't seem that random if I always get the same ones.

like image 433
user1185305 Avatar asked Mar 17 '13 13:03

user1185305


People also ask

How to generate random numbers between 0 and 1 in C++?

C++ Random Number Between 0 And 1 We can use srand () and rand () function to generate random numbers between 0 and 1. Note that we have to cast the output of rand () function to the decimal value either float or double.

How to randomly generate numbers in C++?

rand() rand() function is an inbuilt function in C++ STL, which is defined in header file <cstdlib>. rand() is used to generate a series of random numbers. The random number is generated by using an algorithm that gives a series of non-related numbers whenever this function is called.

Which C++ function generates different random numbers every time a code is executed?

The rand() and srand() functions are used to generate random numbers in C/C++ programming languages. The rand() function gives same results on every execution because the srand() value is fixed to 1.


3 Answers

// 1-st variant: using time() function for seed random distribution
std::default_random_engine generator(time(0));
std::uniform_real_distribution<double> distribution(first, last);
return distribution(generator);

If open multiple programs, with the same random number generator they will all output the same results, because they have the same value of seed which is time.

This issue solved by using random device, in the below code:

// 2-nd variant: 
std::uniform_real_distribution<double> distribution(first, last);
std::random_device rd;
std::default_random_engine generator(rd());
return distribution(generator);
like image 50
spin_eight Avatar answered Oct 20 '22 01:10

spin_eight


If you are referring to the fact that you get the same results for each execution of the program, that's because you need to provide a seed based on some naturally random value (e.g. some number input by the user, or the number of milliseconds elapsed since the computer was turned on, or since January 1, 1970, etc.):

#include <random>

std::default_random_engine generator;
generator.seed( /* ... */ );
//              ^^^^^^^^^    
//              Provide some naturally random value here

std::uniform_real_distribution<float> distribution(0.0, 1.0);

float myrand = distribution(generator);
like image 34
Andy Prowl Avatar answered Oct 20 '22 00:10

Andy Prowl


I have found another good solution...

double Generate(const double from, const double to)
{
    std::random_device rd;

    return std::bind(
        std::uniform_real_distribution<>{from, to},
        std::default_random_engine{ rd() })();
}
like image 34
smela Avatar answered Oct 20 '22 00:10

smela