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.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;
}
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!
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4