Question
Write a C++ function with the following signature: std::vector countRandomFrequencies( unsigned int count, const std::vector & weights) Broadly, the function's job is to choose a
Write a C++ function with the following signature:
std::vectorcountRandomFrequencies( unsigned int count, const std::vector & weights)
Broadly, the function's job is to choose a bunch of random numbers and count how many times each number is chosen.
Specifically, the meanings of the function's parameters are as follows:
- count indicates the number of random numbers that will be chosen and counted.
- weights indicates the possible random numbers that might be chosen, and also how often the various random numbers should be chosen relative to the others so, it is not necessarily true that each possible number has an equal chance of being chosen.
For example, imagine we call the function this way:
std::vectorweights{1, 2, 1, 2, 1, 2}; std::vector frequencies = countRandomFrequencies(99, weights);
Let's examine what this would mean:
- The vector weights consists of six values whose indices are in the range 0 to 5 (inclusive). weights[0], weights[2], and weights[4] have the value 1, while weights[1], weights[3], and weights[5] have the value 2.
- Because the range of indices in the vector weights is 0 to 5 (inclusive), we'll be choosing random numbers in that range (i.e., either 0, 1, 2, 3, 4, or 5).
- The sum of the values in the weights vector is 9.
- The probability of choosing the value i would be weights[i] divided by 9 (the sum of the values in weights). So, for example, we'd have a 1/9 chance of choosing the value 0, a 2/9 change of choosing the value 1, and so on.
- The value of the count parameter to countRandomFrequencies is 99, which means we want to choose 99 random numbers.
The return value, then, would be a vector whose size is the same as weights, containing the number of times each number (0, 1, 2, 3, 4, or 5) was chosen. Of course, since we're choosing 99 random numbers in this example, we would expect the sum of the values in the resulting vector to be 99.
One possible answer that we might expect would be this (because, based on the weights, we expect 1, 3, and 5 to each be chosen twice as often as 0, 2, or 4):
{ 11, 22, 11, 22, 11, 22 }
Though, of course, since you're choosing the numbers randomly, you'd naturally see some variance, though the larger the value of count, the more you'd reasonably expect the result to converge toward what it should be, based on the probabilities in weights.
What you can use
The C++ Standard Library is certainly in play here; your best bet is to pay attention to the techniques demonstrated in the Randomness notes, as well as other things declared in the
documentation
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started