I need to generate random numbers between two specified numbers in a loop with a uniform distribution. I am using the random library of C++11.
My problem is that I keep getting the same number in the loop. It is required that the number generated on every loop pass be different. Code below:
#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include <random>
using namespace std;
double randnum (double a, double b)
{
std::default_random_engine generator;
std::uniform_real_distribution<double> distribution (a,b);
double num=distribution(generator);
return num;
}
int main()
{
int np=100;
double umin =0;
double umax=0.5;
double u[np];
for (int i=0;i<np;i++)
{
u[i]=randnum(umin,umax);
cout<<u[i]<<endl;
}
}
Please help. Any advice on generating random number by any alternative means is welcome, but it must have a uniform distribution.
The random number engines in the Standard Library are pseudo-random, i.e. deterministic in how their internal state is initialized and mutated. This means that each function call will get a fresh new generator that will continue to give the same number over and over again.
Just make the generator a static function variable so that its state can evolve over different function calls.
#include <iostream>
#include <random>
using namespace std;
double randnum (double a, double b)
{
static std::default_random_engine generator;
std::uniform_real_distribution<double> distribution (a,b);
return distribution(generator);
}
int main()
{
const int np=100;
double umin =0;
double umax=0.5;
double u[np];
for (int i=0;i<np;i++)
{
u[i]=randnum(umin,umax);
cout<<u[i]<<endl;
}
}
Live Example.
Note, you will get the same sequence of numbers for each run of your compiled program. To get more random behavior across program invocations, you can use e.g. std::random_device.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With