Plinko program - error with random
Jul 9, 2015 at 8:50pm UTC
Hi, I'm working on a plinko (from the price is right) simulator for my intro to computer programming class. I'm pretty new to coding, so bear with me.
Anyway, I'm writing this program and have everything finished according to the specs except for one thing. When I choose the multiple chip option, instead of all of the chips having different random paths, they all go to the same place. Please help me fix this error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
const int PRIZE_SLOT_0 = 100;
const int PRIZE_SLOT_1 = 500;
const int PRIZE_SLOT_2 = 1000;
const int PRIZE_SLOT_3 = 0;
const int PRIZE_SLOT_4 = 10000;
const int PRIZE_SLOT_5 = 0;
const int PRIZE_SLOT_6 = 1000;
const int PRIZE_SLOT_7 = 500;
const int PRIZE_SLOT_8 = 100;
int winnings;
int drop_simulator (double slot, int print_chip_spot) {
srand(time(NULL));
double chip_spot = slot;
for (int i = 0; i < 12; i++) {
chip_spot += rand() % 2 ? .5: -.5;
if (chip_spot > 8) {
chip_spot -= 1.0;
}
if (chip_spot < 0) {
chip_spot += 1.0;
}
if (print_chip_spot == 1) {
cout << fixed << setprecision(1) << chip_spot << setw(4);
}
}
if (chip_spot == 0) {
winnings = PRIZE_SLOT_0;
} else if (chip_spot == 1) {
winnings = PRIZE_SLOT_1;
} else if (chip_spot == 2) {
winnings = PRIZE_SLOT_2;
} else if (chip_spot == 3) {
winnings = PRIZE_SLOT_3;
} else if (chip_spot == 4) {
winnings = PRIZE_SLOT_4;
} else if (chip_spot == 5) {
winnings = PRIZE_SLOT_5;
} else if (chip_spot == 6) {
winnings = PRIZE_SLOT_6;
} else if (chip_spot == 7) {
winnings = PRIZE_SLOT_7;
} else {
winnings = PRIZE_SLOT_8;
}
return winnings;
}
int main() {
srand(time(NULL));
double slot;
double multi_slot;
bool end;
do {
//Menu
cout << "\t\t\t\t** MENU **\nPlease choose one of the following operations:\n1 - Drop a single chip into one slot\n2 - Drop multiple chips into one slot\n3 - Quit the program\n" ;
int operation;
cin >> operation;
if (operation > 3 || operation < 1) {
cout << "Invalid selection. Please enter 1, 2, or 3 to select one of the options.\n\n" ;
end = false ;
continue ;
}
//Single Chip
if (operation == 1) {
cout << "\n\t\t\t\t\t** DROP SINGLE CHIP **\nPlease enter the number of the slot to place Plinko chip (0-8):\n" ;
cin >> slot;
if (slot < 0 || slot > 8) {
cout << "Invalid selection. Please enter an integer from 0 to 8\n\n" ;
end = false ;
continue ;
} else {
cout << "Path: [" ;
drop_simulator(slot, 1);
cout << "]\nYou won $" << winnings;
}
}
//Multiple Chips
if (operation == 2) {
cout << "\n\t\t **DROP MULTIPLE CHIPS**\nPlease enter number of chips to be placed: " ;
int number_of_chips;
cin >> number_of_chips;
if (number_of_chips <= 0) {
cout << "Invalid number. Please enter a positive integer for number of chips.\n\n" ;
end = false ;
continue ;
} else {
cout << "Please enter the number of the slot to place Plinko chip (0-8): " ;
cin >> slot;
if (slot < 0 || slot > 8) {
cout << "Invalid selection. Please enter an integer from 0 to 8\n\n" ;
end = false ;
continue ;
}
double total_winnings;
total_winnings = 0;
for (int i = 0; i < number_of_chips; i++) {
total_winnings += drop_simulator(slot, 0);
}
cout << "You won $" << total_winnings;
cout << "Average amount earned per chip = $" << total_winnings / number_of_chips;
}
}
end = true ;
} while (end == false );
return 0;
}
Jul 9, 2015 at 8:59pm UTC
1 2 3 4 5 6
for (int i = 0; i < number_of_chips; i++) {
total_winnings += drop_simulator(slot, 0);
}
//...
int drop_simulator (double slot, int print_chip_spot) {
srand(time(NULL));
Never call srand in a loop. It will just reset seed to old value each iteration and all random values will be the same.
Jul 10, 2015 at 3:49am UTC
Awesome, thanks! Problem's fixed now
Topic archived. No new replies allowed.