11.3. Vector of random numbers¶
The first step is to generate a large number of random values and store them in a vector. By “large number,” of course, I mean 20. It’s always a good idea to start with a manageable number, to help with debugging, and then increase it later.
The following function takes a single argument, the size of the vector.
It allocates a new vector of ints, and fills it with random values
between 0 and upper_bound.
std::vector<int> make_vector (std::size_t size, int upper_bound) {
std::vector<int> data (size);
std::random_device r;
std::default_random_engine eng(r());
for (int& value: data) {
value = std::uniform_int_distribution<int> {0, upper_bound} (eng);
}
return data;
}
The return type is std::vector<int>, which means that this function
returns a vector of integers. To test this function, it is convenient to
have a function that outputs the contents of a vector.
void print (const std::vector<int>& data) {
for (const int& value: data) {
cout << value << ' ';
}
}
Notice that it is legal to pass vectors by reference. In fact it
is quite common, since it makes it unnecessary to copy the vector. Since
print does not modify the vector, we declare the parameter
const.
We use the same technique to get values out of the vector without
copying every single one.
Avoiding these kinds of copy operations makes code run faster.
It is also less error prone.
The following code generates a vector and outputs it:
std::size_t num_values = 20;
int upper_bound = 9;
std::vector<int> numbers = make_vector (num_values, upper_bound);
print (numbers);
On my machine the output is
1 9 9 5 9 5 5 2 8 6 2 7 1 0 5 6 2 7 5 6
which is pretty random-looking. Your results might be different.
Try running the active code below!
1#include <cstddef>
2#include <iostream>
3#include <random>
4#include <vector>
5
6// forward declarations
7std::vector<int> make_vector (std::size_t, int);
8void print (const std::vector<int>&);
9
10int main() {
11 std::size_t num_values = 20;
12 int upper_bound = 9;
13 std::vector<int> numbers = make_vector (num_values, upper_bound);
14 print (numbers);
15}
16
17std::vector<int> make_vector (std::size_t size, int upper_bound) {
18 std::vector<int> data (size);
19 std::random_device r;
20 std::default_random_engine eng(r());
21 for (int& value: data) {
22 value = std::uniform_int_distribution<int> {0, upper_bound} (eng);
23 }
24 return data;
25}
26
27void print (const std::vector<int>& data) {
28 for (const int& value: data) {
29 std::cout << value << ' ';
30 }
31}
If these numbers are really random, we expect each digit to appear the same number of times—twice each. In fact, the number 5 appears five times, and the numbers 3 and 4 never appear at all.
Do these results mean the values are not really uniform? It’s hard to tell. With so few values, the chances are slim that we would get exactly what we expect. But as the number of values increases, the outcome should be more predictable.
To test this theory, we’ll write some programs that count the number of
times each value appears, and then see what happens when we increase
num_values.
Q1
How should we declare the parameter, vector, if we don't intend to make any changes to it?
Q2
As we store more and more random numbers in a vector, we expect its contents to be __________.
Q3
Would compiling the following code lead to a compiler error?
1void dostuff (const vector<int> & vec) {
2 for (size_t i = 0; i < vec.size(); i++) {
3 vec[i] = vec[i] ;
4 }
5}