random double Please help me understand this code:

I am working on understanding the following code:

Question 1
To begin with I do not understand what this line does:

uint64_t timeSeed = chrono::high_resolution_clock::now().time_since_epoch().count();

Question 2
Is unif the variable that get created?
uniform_real_distribution<double> unif(0.0, 1.0);
and does a random seed get passed to it each time it is called?
currentRandomNumber = unif(rng);

I found this reference:
https://www.cplusplus.com/reference/random/uniform_real_distribution/

#include <iostream>
#include <random>
#include <chrono>
using namespace std;

int main()
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
{ // Initialize a random seed. This is not beginner code.
mt19937_64 rng; // A Mersenne Twister pseudo-random generator of 64-bit numbers with a state size of 19937 bits.
// initialize the random number generator with time-dependent seed
uint64_t timeSeed = chrono::high_resolution_clock::now().time_since_epoch().count();
seed_seq ss{uint32_t(timeSeed & 0xffffffff), uint32_t(timeSeed>>32)};
rng.seed(ss);
// initialize a uniform distribution between 0 and 1
uniform_real_distribution<double> unif(0.0, 1.0);
// ready to generate random numbers
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

int count = 0;
double currentRandomNumber = 0.0;
const int nSimulations = 1000;
for (int i = 0; i < nSimulations; i++)
{
currentRandomNumber = unif(rng);
cout << currentRandomNumber<<" ";
if(i % 15 == 0 && i != 0)cout<<endl;
}
return 0;
}
Use code tags when posting code.
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting indentation would not hurt either

Your code with code tags, and a bit of indentation formatting:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <random>
#include <chrono>

int main()
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
{ // Initialize a random seed. This is not beginner code.
   std::mt19937_64 rng; // A Mersenne Twister pseudo-random generator of 64-bit numbers with a state size of 19937 bits.

   // initialize the random number generator with time-dependent seed
   uint64_t timeSeed = std::chrono::high_resolution_clock::now().time_since_epoch().count();

   std::seed_seq ss { uint32_t(timeSeed & 0xffffffff), uint32_t(timeSeed >> 32) };

   rng.seed(ss);

   // initialize a uniform distribution between 0 and 1
   std::uniform_real_distribution<double> unif(0.0, 1.0);

   // ready to generate random numbers
   // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
   int count { };
   double currentRandomNumber { };
   const int nSimulations { 1000 };

   for (int i { 1 }; i <= nSimulations; i++)
   {
      currentRandomNumber = unif(rng);

      std::cout << currentRandomNumber << ' ';

      if (i % 7 == 0) std::cout << '\n';
   }
   std::cout << '\n';
}

Line 11 is one way in C++ to generate a pseudo-random number seed, using the high resolution clock from <chrono>. The value returned is the current time.

The C method is time().

Line 13 creates a seed sequence object. A general-purpose bias-eliminating scrambled seed sequence generator.
https://en.cppreference.com/w/cpp/numeric/random/seed_seq

Line 18 creates a distribution object that RETURNS a pseudo-random number when "calling" the object's operator(). Passing the PRNG you created at line 8 as a parameter.

The distribution object is called at line 28.

FYI, there are easier ways to seed and use a PRNG:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <random>

int main()
{
   // https://en.cppreference.com/w/cpp/numeric/random/random_device
   std::default_random_engine PRNG(std::random_device {}());

   std::uniform_real_distribution<double> dist(0.0, 1.0);

   const int nSimulations { 1000 };

   for (int i { 1 }; i <= nSimulations; i++)
   {
      std::cout << dist(PRNG) << ' ';

      if (i % 7 == 0) std::cout << '\n';
   }
   std::cout << '\n';
}
Thank you so much!
And the links to format my questions.
There will be more to come.
1
2
3
4

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/


It looks like PRNG is an OBJECT ?
 
std::default_random_engine PRNG(std::random_device {}());


Would std::default_random_engine create the OBJECT PRNG?

I discovered I could change the name or PRNG to RNG.
aka pseudo-random number generator
Last edited on
PRNG is an instance of the template class std::default_random_engine. You do know about C++ classes and templates?

You can change the name of an instantiated object to anything you like, as long as you don't use a reserved C++ keyword. I called it PRNG 'cuz I was lazy. PRNG = Pseudo Random Number Generator.

C++ does random number generation differently than C, with almost too many different tools available. It can be intimidating to someone learning.

If one is creating C apps they are kinda "stuck" with the <stdlib.h> functions srand() and rand(). But there are problems with how pseudo-random numbers are generated that C++ tried to correct.
https://web.archive.org/web/20180123103235/http://cpp.indi.frih.net/blog/2014/12/the-bell-has-tolled-for-rand/

https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful

For a history of generating random numbers Learn C++ offers this:
https://www.learncpp.com/cpp-tutorial/random-number-generation/

A paper on C++11 random number generation:
http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3551.pdf
1
2
 You do know about C++ classes and templates?


I am just leaning about classes and templates. I need to study template class
I've done this in Java.

https://www.cplusplus.com/doc/oldtutorial/templates/

Thank you so much for the professional explanation!!!


It's time for this unit: https://www.cplusplus.com/doc/tutorial/classes/
Topic archived. No new replies allowed.