Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** C++ headers **/
- #include <iostream>
- #include <vector>
- #include <limits>
- /** C headers **/
- #include <ctime>
- /** constants **/
- #define DICESIDES 6
- // the maximum number an integer can hold on my system.
- #define NUM_SIMS std::numeric_limits<int>::max()
- // generate a random integer (through the standard C rand function) bound by a
- // lower and upper value
- int getRandomInt(int min, int max)
- {
- return rand() % (max + 1 - min) + min;
- }
- int main (int argc, char * const argv[]) {
- // Set the random seed to time.
- srand(time(NULL));
- // making a "grab-bag" of dice values. This could theoretically be used for
- // any D[X] dice simulation.
- std::vector<int> singleDie (DICESIDES);
- for(int i = 0; i < DICESIDES; i += 1) {
- singleDie.at(i) = i + 1;
- }
- // lets keep track of all these
- std::vector<int> results (DICESIDES);
- for(int i = 0; i < DICESIDES; i += 1) {
- results.at(i) = 0;
- }
- // lets simulate NUM_SIMS rolls.
- for(int i = 0; i < NUM_SIMS; i += 1) {
- for(int j = 0; j < DICESIDES; j += 1) {
- int roll = getRandomInt(0, DICESIDES - 1);
- int temp = singleDie.at(j);
- singleDie.at(j) = singleDie.at(roll);
- singleDie.at(roll) = temp;
- }
- results.at(singleDie.at(0) - 1) += 1;
- // every billion iterations
- if(i % 1000000000 == 0 && i > 0) {
- std::cout << i << " iterations." << std::endl;
- }
- }
- // Print the results
- std::cout << "Number of simulations: " << NUM_SIMS << std::endl;
- for(int i = 0; i < DICESIDES; i += 1) {
- float percentage = ((float)(results.at(i)) / (float)(NUM_SIMS)) * 100.0f;
- std::cout << "Result: " << i + 1 << ": " << results.at(i) << " (" << percentage
- << "%)" << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement