This post is a short one explaining the boost::random with fixed seed and with variable seed.
First of all let me explain how it works. You use an random engine that is initialized with a seed. After that you have your distribution object (here I use uniform integer distribution) where you set the min max range. Note that the max number is included so you get numbers from 0 to and including 9 here. Now you just pass the generator to the distribution object and it will spit out a number.
The numbers are deterministic meaning if you restart your programm with the same seed the sequence will be the same every time.
To get a random number you just pass the generator to the distribution again and it will give you the next number in the sequence.
If you want to have different numbers every startup you have to include some variables in your seed that change every time. Like the actual time. This is done in the first example when you pass the seed 0.
#include "boost/random/mersenne_twister.hpp" #include "boost/random/uniform_int_distribution.hpp" #include <ctime> int getGeneratedNumber(int seed) { int randomNumber = 0; if (seed == 0) { time_t rawtime = time(0); seed = rawtime; } Sudokugenerator::usedSeed = seed; boost::random::mt11213b randomgen(seed); boost::random::uniform_int_distribution<> dist(0,9); randomNumber = dist(randomgen); return randomNumber; }
If you don’t care about everything and just want random numbers every time you call a function this one might be for you. Note that this will return always the same numbers when restarting the programm. If you don’t want that you have to initialize the randomgenerator with a variable seed, explained in the first example.
#include "boost/random/mersenne_twister.hpp" #include "boost/random/uniform_int_distribution.hpp" // global const for demo purpose boost::random::mt11213b RANDOMGEN(seed) int getNumber() { int randomNumber = 0; boost::random::uniform_int_distribution<> dist(0,9); randomNumber = dist(RANDOMGEN); return randomNumber; }
Archives
- November 2023
- December 2022
- November 2022
- February 2022
- November 2021
- October 2021
- September 2021
- July 2021
- April 2021
- March 2021
- February 2021
- January 2021
- September 2020
- July 2020
- April 2020
- March 2020
- February 2020
- December 2019
- November 2019
- October 2019
- August 2019
- June 2019
- February 2019
- December 2018
- November 2018
- October 2018
- September 2018
- August 2018
- July 2018
- May 2018
- March 2018
- February 2018
- December 2017
- November 2017
- September 2017
- July 2017
- June 2017
- April 2017
- February 2017
- January 2017
- October 2016
- September 2016
- July 2016
- May 2016
- April 2016
- March 2016
- August 2015
- July 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- October 2014
- April 2014
- March 2014
- February 2014
Calendar
M | T | W | T | F | S | S |
---|---|---|---|---|---|---|
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 |