Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random class for c++

Tags:

c++

random

I want for a project in C++, to have a class that has the functionality that Random class has in java or in c#

I have found this one, http://www.dreamincode.net/code/snippet342.htm but it has bugs and I can't quite fix them right now.

Could you point out the bugs and their fixes, or suggest another implementation?

like image 415
George Kastrinis Avatar asked Feb 28 '26 05:02

George Kastrinis


1 Answers

There are three nearly identical, high-quality "standard" random number generation libraries that you should try to find in descending order:

  • C++11's <random>.
  • The TR1's <tr1/random>
  • Boost's <boost/random.hpp>.

They're all conceptually identical and even practically near-identical, apart from the namespace (std, std::tr1 and boost, respectively).

Each library defines a set of engines, such as std::mt19937. Pick one (for each thread) and seed it.

Once you have an engine, you can use a wide variety of distributions to generate numbers, using your engine. Frequently used distributions are uniform integers in a range [a, b], uniform floats in the range [0,1), and several well-known probability distributions like the normal distribution.

like image 51
Kerrek SB Avatar answered Mar 01 '26 18:03

Kerrek SB