What does this code mean, Random numbers

1
2
3
4
5
6
7
#include <random>

std::random_device rd;
std::default_random_engine generator(rd()); // rd() provides a random seed
std::uniform_real_distribution<double> distribution(0.1,10);

double number = distribution(generator);


I got this off the internet to use in my program. I know it generates random numbers and I've got it working but I don't know what this code means. I looked at the random header where I found all these commands but I cant seem to understand the syntax. Can someone explain what each line does?
You may want to start with rand() and srand() before jumping into more advanced PRNGs(pseudo random number generators).

As far as your code a std::random_device is a generator for a "random" number. It is supposed to be a "true random" number generator and I don't think is supported on some compilers. std::default_random_engine generator is another PRNG which takes a seed as a constructor. So basically you are picking a random number to see this generator. std::uniform_real_distribution<double> This is just a uniform distribution which makes it so that each "random" number will appear equally likely each time it is called.


This makes thing a little clearer albeit still a bit advanced for me I guess. Thanks for the help.
Topic archived. No new replies allowed.