public member function
<random>

std::uniform_int_distribution::(constructor)

(1)
explicit uniform_int_distribution ( result_type a = 0,
                                    result_type b = numeric_limits<result_type>::max() );
(2)
explicit uniform_int_distribution ( const param_type& parm );
Construct uniform discrete distribution
Constructs a uniform_int_distribution object, adopting the distribution parameters specified either by a and b or by object parm.

Parameters

a, b
Upper and lower bounds of the range ([a,b]) of possible values the distribution can generate.
Note that the range includes both a and b (along with all the integer values in between).
b shall be greater than or equal to a (a<b).
result_type is a member type that represents the type of the random numbers generated on each call to operator(). It is defined as an alias of the first class template parameter (IntType).
parm
An object representing the distribution's parameters, obtained by a call to member function param.
param_type is a member type.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// uniform_int_distribution example
#include <iostream>
#include <chrono>
#include <random>

int main()
{
  // construct a trivial random generator engine from a time-based seed:
  unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
  std::default_random_engine generator (seed);

  std::uniform_int_distribution<int> distribution(1,100);

  int guess;
  int number = distribution(generator);

  while (true) {
    std::cout << "guess the number (between 1 and 100): ";
    std::cin >> guess;
    if (number==guess) {std::cout << "right!\n"; break; }
    else if (number>guess) std::cout << "it's greater\n";
    else std::cout << "it's less\n";
  }

  return 0;
}


Possible output:
guess the number (between 1 and 100): 50
it's greater
guess the number (between 1 and 100): 75
it's greater
guess the number (between 1 and 100): 87
it's less
guess the number (between 1 and 100): 81
it's less
guess the number (between 1 and 100): 78
it's less
guess the number (between 1 and 100): 76
right!

Complexity

Constant.

See also