Let's build upon what we know about random number generation to distribute numbers randomly. We'll achieve this by first generating n
numbers between 0 and 100. If we add these together we get a random total where each of our individual numbers represents a percentage of that. We can then take that percentage of our goal number to get a random portion. The following code demonstrates this and will make it clearer.
You can download the code for this program from the Packt website. It will be in the Examples
folder, and the project name is random_distribution
:
#include <iostream> using namespace std; // Entry method of the application. int main() { // Create and initialize our variables. int upperLimit = 0; // Output instructions. cout << "Enter a number, and we'll split it into three random smaller numbers:" << endl; cin >> upperLimit; cout << endl; float number1Bias = rand() % 101; float number2Bias = rand() % 101...