How can i used boost library in C++ to generate random numbers between 1 and 9999
Asked
Active
Viewed 817 times
-6
-
You need to use the modulus operator `%` (think a little bit). And you might not need boost, just use the standard `random()` or `lrand48()` functions. – Basile Starynkevitch Feb 06 '12 at 06:11
-
2Go and read the boost documentation at http://www.boost.org/doc/libs/1_49_0_beta1/doc/html/boost_random.html and come back with an actual question when you've actually tried something and gotten stuck. – Yuushi Feb 06 '12 at 06:12
1 Answers
1
Did you try googling for "boost random number" first? Here's the relevant part of their documentation generating boost random numbers in a range
You want something like this:
#include <time.h>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
std::time(0) gen;
int random_number(start, end) {
boost::random::uniform_int_distribution<> dist(start, end);
return dist(gen);
}
edit: this related question probably will answer most of your questions: why does boost::random return the same number each time?