New to C++. I'm trying to understand RNG and how to produce random numbers in C++. I'm not sure what is the mistake of my code, but it gave me gibberish numbers of counts for each number, except one or two. Thanks for help
include <iostream>
#include <random>
int main()
{
const size_t nonums {100};
const size_t maxno {10};
int counter[maxno + 1] {};
std::mt19937 gen(std::random_device {}());
std::uniform_int_distribution <int> num(0, maxno);
for (int k = 0; k < nonums; ++counter[num(gen)], ++k);
size_t throws {};
for (int k = 0; k <= maxno; throws += counter[k], ++k)
std::cout << " Number " << k << " has appeared " << counter[k] << " times." << "\n";
std::cout << throws << " throws\n";
}
Number 0 has appeared 8 times.
Number 1 has appeared 11 times.
Number 2 has appeared 6 times.
Number 3 has appeared 10 times.
Number 4 has appeared 6 times.
Number 5 has appeared 12 times.
Number 6 has appeared 10 times.
Number 7 has appeared 15 times.
Number 8 has appeared 9 times.
Number 9 has appeared 8 times.
Number 10 has appeared 5 times.
100 throws
If gen is required to be used in different functions, then I place it once at global level (IMO one of the few uses for a global variable). If a distribution is required only within a function, then they can be specified as static within that function. Eg:
random_device doesn't work on quite a lot of systems (including cpp.sh).
From the reference on this site:
Notice that random devices may not always be available to produce random numbers (and in some systems, they may even never be available). This is signaled by throwing an exception derived from the standard exception on construction or when a number is requested with operator().
Unless the program really requires a stochastic process to generate random numbers, a portable program is encouraged to use an alternate pseudo-random number generator engine instead, or at least provide a recovery method for such exceptions.
a software provided 'device' is no better than the other method, though: its doing the exact same kinds of things as the engines. the devices are for things like the lottery, which feeds something like a geiger counter to the software for the seed so no one can easily cheat the system.
> random_device doesn't work on quite a lot of systems
std::random_device as a random number generator that produces non-deterministic random numbers is currently available on all mainstream implementations.
random_device doesn't work on quite a lot of systems
I mentioned that in the past, that std::random_device isn't fully implemented with all compilers, and was "informed" I was wrong. It works with VS, it still doesn't work AFAIK with MinGW/GCC. I don't know about any other compiler since I really use only VS and MinGW/GCC.
If I want a time based seed for <random> I use the system clock in <chrono>, using the C library time function doesn't have the granularity.
Most times I seed using a seed sequence made from std::random_device and std::chrono::system_clock. The implementation details hidden in a header file simple toolkit, so I can obtain C++ random numbers almost as easily as using the C library by including a header file.
#include <iostream>
#include "random_toolkit.hpp"
int main()
{
// let's seed the random engine
rtk::srand();
// let's get some int random numbers
for (int i { }; i < 10; ++i)
{
std::cout << rtk::rand(-10, 25) << ' ';
}
std::cout << "\n\n";
// let's get some double random numbers
for (int i { }; i < 10; ++i)
{
std::cout << rtk::rand(-1.5, 1.5) << '\n';
}
}
Now I don't have to deal with the messy details of implementation if I want to generate random numbers "The C++ way." No matter the compiler and issues with std::random_device. I include my custom header and away we go!
There are other details of my custom header file that are for a different discussion.
Re 'Simple random number generation facilities' in Library Fundamentals v2 ( ISO/IEC TS 19568:2017)
These don't seem to have been implemented in VS 2019 as the folder experimental/random is not present. Also MSDN documentation does not seem to refer to randint() or reseed(). Ahhh...