-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom-number.cpp
More file actions
61 lines (49 loc) · 1.43 KB
/
random-number.cpp
File metadata and controls
61 lines (49 loc) · 1.43 KB
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
60
61
/** @file random-number.cpp
* @brief
*
* You have a function rand7() that generates a random integer from 1 to 7. Use it to write a function rand5() that generates a random integer from 1 to 5.
*
* rand7() returns each integer with equal probability. rand5() must also return each integer with equal probability.
*
* @author Shawn Mullings (@shawnmullings)
* @bug No known bugs.
*/
/* -- Includes -- */
#include <iostream>
#include <random>
#include <ctime>
#include <math.h>
/*
*
* Generates a random integer from 1 to 7
*
* return integer from 1 - 7
*/
int rand7(){
/* Uniformly-distributed integer random number generator that produces non-deterministic random numbers */
std::random_device rd;
/* Mersenne Twister pseudo-random generator of 32-bit numbers */
std::mt19937 gen(rd());
/* Produce random integer value uniformly distributed on the interval 1 - 7 */
std::uniform_int_distribution<> dis(1, 7);
return dis(gen);
}
/*
*
* Generates a random integer from 1 to 5
*
* return integer from 1 - 5
*/
int rand5(){
/* 5 / 7 = 0.71428571. Use to squeeze range 1 - 7 into range 1 - 5 */
float tUnits = 0.71428571;
/* 5 / 7 = 0.71428571 */
return ( std::ceil(tUnits * rand7()) );
}
int main(int argc, char const *argv[])
{
/* Generate rand5() using rand7() ouput */
std::cout << "Generate random integer from 1 to 5" << std::endl;
std::cout << "Random integer: " << rand5() << std::endl;
return 0;
}