1

I'm generating random numbers in C++11. When I run my code

using namespace std;

static std::mt19937_64 rng;

int main() {
rng.seed(11);

    std::cout << rng() << std::endl;
}

It returns random numbers. However, I would like to restrict this to a range of numbers, say 1-1000. How would I do this, using the new <random> file/library introduced in C++11?

hichris123
  • 10,145
  • 15
  • 56
  • 70
  • 3
    use the mod operator `%` – andre Jul 15 '13 at 21:04
  • 8
    If you want them uniformly distributed, http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution. – chris Jul 15 '13 at 21:05
  • 7
    @andre Nope, the modulus operator doesn't generate a uniform distribution of numbers, check it out: http://www.cplusplus.com/reference/cstdlib/rand/ –  Jul 15 '13 at 21:06
  • Try [this question](http://stackoverflow.com/q/2509679/2417578) -- noting that it's a different language, so _not_ a dup (although those answers would do just fine). – sh1 Jul 15 '13 at 21:06
  • `%` is strictly speaking not correct, to see that, assume the complete range was [0, 100) if you would `% 80` random numbers from this range, the result would no longer be uniformly distributed. – Micha Wiedenmann Jul 15 '13 at 21:07
  • @LukeSanAntonio the documentation says its not uniform, then goes right along with the sample using it. – Justin Meiners Jul 15 '13 at 21:07
  • @JustinMeiners: It shows the sample, then explains why you shouldn't use it if you want a uniform distribution, and says "C++ supports a wide range of powerful tools to generate random and pseudo-random numbers (see for more info)." If you follow the link to ``, it gives you the right answer immediately. – abarnert Jul 15 '13 at 21:08
  • @abarnert is there a standard library solution for C? – Justin Meiners Jul 15 '13 at 21:09
  • @LukeSanAntonio I was aware that it was not truly uniform and it was worse the larger the number was. However, I was not aware we had a better option `std::uniform_int_distribution`, thanks. – andre Jul 15 '13 at 21:11
  • @andre Great! Glad to help, if I did. –  Jul 15 '13 at 21:14
  • @JustinMeiners: No, C99 only has `rand()` (POSIX adds `rand_r`, and `random`, but neither of those helps); you will need to go outside the stdlib to do it. There's a linked answer on the right, and plenty of other answers on SO and google, or you can download a PRNG library. – abarnert Jul 15 '13 at 21:23
  • @JustinMeiners, There's a good question on that here. Just keep generating random numbers until they fall into the range of equally-likely ones, i.e., not the top X numbers that can cause bias. – chris Jul 15 '13 at 21:26

1 Answers1

17

Use std::uniform_int_distribution from <random>.

Example:

#include <random>
#include <iostream>

int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<int> dis(1, 1000);

    std::cout << dis(gen) << '\n';
}

The <random> header includes a lot of other distributions that you can use. You can check it out here

Rapptz
  • 20,807
  • 5
  • 72
  • 86