-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomNumberInitialization.cpp
49 lines (39 loc) · 1.03 KB
/
RandomNumberInitialization.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
/**
* \file RandomNumberInitialization.cpp
* \brief
*
* \todo
*/
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <algorithm>
struct RandomGenerator {
int maxValue;
RandomGenerator(int max) :
maxValue(max) {
}
int operator()() {
return rand() % maxValue;
}
};
int main() {
// Initialize a vector with 10 ints of value 0
std::vector<int> vecOfRandomNums(10);
// Generate 10 random numbers by lambda func and fill it in vector
std::generate(vecOfRandomNums.begin(), vecOfRandomNums.end(), []() {
return rand() % 100;
});
std::cout << "Random Number Generated by Lambda Function" << std::endl;
for (int val : vecOfRandomNums)
std::cout << val << " ";
std::cout << "\n" << std::endl;
// Generate 10 random numbers by a Functor and fill it in vector
std::generate(vecOfRandomNums.begin(), vecOfRandomNums.end(),
RandomGenerator(500));
std::cout << "Random Number Generated by Functor" << std::endl;
for (int val : vecOfRandomNums)
std::cout << val << " ";
std::cout << "" << std::endl;
return 0;
}