RNG

There was once a time in my life when I could wright an RNG that used the <random> and <chrono>.

it looked something like

unsigned seed;
m19937(seed)systemclock_something_blah_something
distribution generator(1,6)

but know I cant remember, can anyone help with this.
but know I cant remember
In case you know not now... have a look to the TOC to the left of this site. -- http://www.cplusplus.com/reference/random/
Just plop the following into a header file, I called mine "random_toolkit.hpp," include it whenever you need random numbers, and you will not need to remember how to set up a C++ RNG:

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* A simple toolkit to help beginners using <random> library an easier task */

// shamelessly stolen and adapted from a C++ working paper: WG21 N3551
// http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3551.pdf

#ifndef __RANDOM_TOOLKIT_HPP__
#define __RANDOM_TOOLKIT_HPP__

#include <random>
#include <chrono>

namespace rtk
{
   inline std::default_random_engine& urng()
   {
      static std::default_random_engine URNG { };
      return URNG;
   }

   inline void srand()
   {
      static unsigned seed   = static_cast<unsigned> (std::chrono::system_clock::now().time_since_epoch().count());
      static bool     seeded = false;

      if (!seeded)
      {
         urng().seed(seed);

         seeded = true;
      }
   }

   inline void srand(unsigned seed)
   {
      static bool seeded = false;

      if (!seeded)
      {
         urng().seed(seed);

         seeded = true;
      }
   }

   // two function overloads to obtain uniform distribution ints and doubles
   inline int rand(int from, int to)
   {
      static std::uniform_int_distribution<> dist { };

      return dist(urng(), decltype(dist)::param_type { from, to });
   }

   inline double rand(double from, double to)
   {
      static std::uniform_real_distribution<> dist { };

      return dist(urng(), decltype(dist)::param_type { from, to });
   }

   // function for rolling dice, and checking if the # of pips is nonstandard
   inline int roll_die(int pips)
   {
      //check to see if the number of die pips is less than 2
      if (pips < 2)
      {
         return 0;
      }

      return rand(1, pips);
   }
}

#endif 


Then you can mash together code almost as easily as using the C library srand/rand functions:

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <array>
#include <iostream>
#include <numeric>
#include <string>
#include "random_toolkit.hpp"

// function for flipping a coin, returning a std::string
std::string flip_coin()
{
   return (rtk::rand(0, 1) ? "Heads" : "Tails");
}

int main()
{
   // create a random engine and randomize it
   rtk::srand();

   using card = unsigned short;

   // manufacture a deck of cards:
   std::array<card, 52> deck;

   // create the cards, 0 (zero) to 51
   std::iota(deck.begin(), deck.end(), 0);

   // shuffle the deck:
   std::shuffle(deck.begin(), deck.end(), rtk::urng());

   // lambdas to display each card in the shuffled deck:
   auto rank = [](card c) { return "AKQJT98765432"[c % 13]; };
   auto suit = [](card c) { return "SHDC"[c / 13]; };

   card count = 0;

   for (card c : deck)
   {
      std::cout << rank(c) << suit(c) << ' ';
      count++;

      if (0 == (count % 13))
      {
         std::cout << '\n';
      }
   }
   std::cout << '\n';

   for (unsigned loop = 0; loop < 50; loop++)
   {
      std::cout << rtk::roll_die(6) << ' ';

      if ((loop + 1) % 20 == 0)
      {
         std::cout << '\n';
      }
   }
   std::cout << "\n\n";

   for (unsigned loop = 0; loop < 25; loop++)
   {
      std::cout << flip_coin() << '\t';

      if ((loop + 1) % 8 == 0)
      {
         std::cout << '\n';
      }
   }
   std::cout << '\n';

   std::cout << "\nLet's see if we can have a non-standard die.....\nA die with 1 side: ";
   std::cout << rtk::roll_die(1) << '\n';

   std::cout << "A die with negative sides: ";
   std::cout << rtk::roll_die(-6) << '\n';
}
5D 9C 5H QD TH QH 7H 6H 7C QS JD 4C 5C
3H QC 8H AS KC AH 3C 8D 2D KD 2H 3D AD
4S TS TD KS 6S JH JC 2C 5S 9S 4D 7S 9D
4H 7D 8C TC 3S 6C JS 2S 6D KH 8S 9H AC

3 4 5 4 2 2 5 2 6 1 1 3 4 3 4 2 2 1 2 2
6 6 2 6 1 1 3 2 5 6 4 2 4 6 5 6 3 6 4 2
3 4 5 4 2 1 4 6 2 5

Heads   Tails   Tails   Heads   Tails   Tails   Heads   Heads
Tails   Heads   Tails   Tails   Heads   Tails   Heads   Heads
Heads   Heads   Tails   Tails   Tails   Tails   Tails   Tails
Tails

Let's see if we can have a non-standard die.....
A die with 1 side: 0
A die with negative sides: 0
Last edited on
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <chrono>
#include <random>
int main (){
	unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();	 
	std::minstd_rand0 g(seed);
	std::cout << g();
return 0;
}
Topic archived. No new replies allowed.