I'm trying to create a program that rolls a random number 1-6, which is then outputted to a die face, then it stops after each number has been rolled once. I figured majority of the code out, just don't know where to insert my break statements. What should I do?
**CODE BELOW**
// Constants
const int MIN_VALUE = 1; // Min number the die can be
const int MAX_VALUE = 7; // Max number the die can be
// Variables
int randomNumber;
// System time
unsigned seed = time(0);
// Seed the random number generator
srand(seed);
cout << "The number rolled is:" << endl;
randomNumber = (rand() % (MAX_VALUE - MIN_VALUE + 1));
cout << randomNumber << endl << "---------------" << endl;
if (randomNumber == 1) // If a 1 is drawn, output X7
{
OutputData(32);
Sleep(2000);
cout << "---------------" << endl;
}
else if (randomNumber == 2) // If a 2 is drawn, output X1 and X6
{
OutputData(9);
Sleep(2000);
cout << "---------------" << endl;
}
else if (randomNumber == 3) // If a 3 is drawn, output X1, X6 and X7
{
OutputData(41);
Sleep(2000);
cout << "---------------" << endl;
}
else if (randomNumber == 4) // If a 4 is drawn, output X1, X3, X4 and X6
{
OutputData(89);
Sleep(2000);
cout << "---------------" << endl;
}
else if (randomNumber == 5) // If a 5 is drawn, output X1, X3, X4, X6, and X7
{
OutputData(121);
Sleep(2000);
cout << "---------------" << endl;
}
else if (randomNumber == 6) // If a 6 is drawn, output X1, X2, X3, X4, X5, and X6
{
OutputData(95);
Sleep(2000);
cout << "---------------" << endl;
}
}
#include <iostream>
#include <random>
#include <algorithm>
#include <numeric>
void OutputData(unsigned n) { std::cout << " output " << n << '\n'; } // For testing
std::mt19937 rng(std::random_device {}());
int main() {
constexprunsigned MIN_VALUE {1}; // Min number the die can be
constexprunsigned MAX_VALUE {6}; // Max number the die can be
constexprunsigned odata[MAX_VALUE - MIN_VALUE + 1] {32, 9, 41, 89, 121, 95};
unsigned nos[MAX_VALUE - MIN_VALUE + 1];
std::iota(std::begin(nos), std::end(nos), 1);
std::shuffle(std::begin(nos), std::end(nos), rng);
for (auto n : nos) {
std::cout << "The number rolled is: " << n;
OutputData(odata[n - MIN_VALUE]);
}
}
The number rolled is: 1 output 32
The number rolled is: 6 output 95
The number rolled is: 5 output 121
The number rolled is: 2 output 9
The number rolled is: 3 output 41
The number rolled is: 4 output 89
Thanks @jonnin. I have edited to accommodate that. But there are still various possible orders (because the diagonal could go the other way, for example).
I just wondered why the student was outputting such odd numbers.
#include <iostream>
#include <random>
#include <bitset>
void OutputData(unsigned n) { std::cout << " output " << n << '\n'; } // For testing
std::mt19937 rng(std::random_device {}());
int main() {
constexprunsigned MIN_VALUE {1}; // Min number the die can be
constexprunsigned MAX_VALUE {6}; // Max number the die can be
constexprunsigned odata[MAX_VALUE - MIN_VALUE + 1] {32, 9, 41, 89, 121, 95};
const std::uniform_int_distribution<unsigned> distrib(MIN_VALUE, MAX_VALUE);
std::bitset<MAX_VALUE - MIN_VALUE + 1> bits;
while (!bits.all()) {
constauto val {distrib(rng)};
bits.set(val - MIN_VALUE);
std::cout << "The number rolled is: " << val;
OutputData(odata[val - MIN_VALUE]);
}
}
The number rolled is: 3 output 41
The number rolled is: 3 output 41
The number rolled is: 2 output 9
The number rolled is: 2 output 9
The number rolled is: 1 output 32
The number rolled is: 4 output 89
The number rolled is: 5 output 121
The number rolled is: 1 output 32
The number rolled is: 5 output 121
The number rolled is: 3 output 41
The number rolled is: 6 output 95