I would like to generate a random number between 0 and 3 and I have the following in my code:
int random = rand() % 4;
This works fine but
You can use the discrete_distribution class from the random library.
#include
#include
#include
int main()
{
std::discrete_distribution<> dist({ 1.0, 4.0, 4.0, 4.0 });
std::mt19937 eng(std::time(0));
for (int i=0; i<100; ++i)
std::cout << dist(eng);
}
Demo: http://ideone.com/z8bq4
If you can't use C++11, these classes also exist in boost.