What is the simplest way to use C++ random library?

Pages: 12
closed account (E0p9LyTq)
@Cubbi, that is sad. Sad that MinGW is not fixing what they know is a bug.

It was several years ago that I started using TDM-GCC 4.9.2 and what at the time I believed was GCC 5.1. I ran across the std::random_device bug and started looking around for better alternatives. I was also less understanding of the differences between compilers.

I finally settled on MSVS, mostly because of the debugging. Any C++ features MSVC hasn't at this time implemented I haven't done any experimentation. Right now I'm delving into other aspects of C++11/C++14 I know MSVC has implemented.
Just to be clear, it's not strictly a bug for random_device to always give the same sequence of numbers as long as entropy() == 0.

If all you need is a single-value seed it's easy to use the time as a fallback.

 
std::size_t seed = std::random_device{}() ^ std::time(nullptr);

This works because a Non-random value XOR Random value => Random value.
it's not strictly a bug for random_device to always give the same sequence of numbers

To me as a user, though, unacceptable quality of implementation is a bug just the same.
closed account (E0p9LyTq)
Cubbi wrote:
To me as a user, though, unacceptable quality of implementation is a bug just the same.

My opinion as well.

@Sanboro, have you looked at the C++ working paper I linked to? There is a proposal for a very simple toolkit for using the C++ <random> library to replace rand()/srand(). All you need to do is include the toolkit source in your project and you get the robustness of C++ with a very simple and easy to use interface.

rand_toolkit.hpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef __RAND_TOOLKIT_HPP__
#define __RAND_TOOLKIT_HPP__

#include <random>
#include <chrono>

std::default_random_engine& urng();

void randomize();

int pick_a_number(int from, int to);

double pick_a_number(double from, double to);

#endif 


rand_toolkit.cpp:
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
#include "rand_toolkit.hpp"

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

void randomize()
{
   unsigned seed = static_cast<unsigned> (std::chrono::system_clock::now().time_since_epoch().count());
   urng().seed(seed);
}

int pick_a_number(int from, int to)
{
   static std::uniform_int_distribution<> dist {};
   using param_type = decltype(dist)::param_type;
   return dist(urng(), param_type {from, to} );
}

double pick_a_number(double from, double to)
{
   static std::uniform_real_distribution<> dist {};
   using param_type = decltype(dist)::param_type;
   return dist(urng(), param_type {from, to});
}


To use the toolkit, simply include the header and source file in your project:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <random>
#include <chrono>

#include "rand_toolkit.hpp"

int main( )
{
   // randomize the toolkit
   randomize();
   
   std::cout << "Rolling 100 dice....\n";
   
   for (size_t roll = 0; roll < 100; roll++)
   {
      int die = pick_a_number(1, 6);
      
      std::cout << die << "\t";
   }
   std::cout << "\n";      
}
Topic archived. No new replies allowed.
Pages: 12