-
Notifications
You must be signed in to change notification settings - Fork 3k
/
ex17_28_29_30.cpp
59 lines (54 loc) · 1.6 KB
/
ex17_28_29_30.cpp
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/***************************************************************************
* @file main.cpp
* @author Alan.W
* @date 7 Mar 2014
* @remark This code is for the exercises from C++ Primer 5th Edition
* @note
***************************************************************************/
//
// Exercise 17.28:
// Write a function that generates and returns a uniformly distributed random
// unsigned int each time it is called.
//
// Exercise 17.29:
// Allow the user to supply a seed as an optional argument to the function you
// wrote in the previous exercise.
//
// Exercise 17.30:
// Revise your function again this time to take a minimum and maximum value for
// the numbers that the function should return.
//
#include <iostream>
#include <random>
#include<string>
// default version
unsigned random_gen();
// with seed spicified
unsigned random_gen(unsigned seed);
// with seed and range spicified
unsigned random_gen(unsigned seed, unsigned min, unsigned max);
int main()
{
std::string temp;
while(std::cin >> temp)
std::cout << std::hex << random_gen(19, 1, 10) << std::endl;
return 0;
}
unsigned random_gen()
{
static std::default_random_engine e;
static std::uniform_int_distribution<unsigned> ud;
return ud(e);
}
unsigned random_gen(unsigned seed)
{
static std::default_random_engine e(seed);
static std::uniform_int_distribution<unsigned> ud;
return ud(e);
}
unsigned random_gen(unsigned seed, unsigned min, unsigned max)
{
static std::default_random_engine e(seed);
static std::uniform_int_distribution<unsigned> ud(min, max);
return ud(e);
}