Feb 7, 2013 at 3:46am Feb 7, 2013 at 3:46am UTC
I am doing this plinko lab. If you don't know what plinko is, here is the picture :
http://www.hoytcompany.com/images/games/plinko.jpg
Multiple chips are inserted into one slot and each ship is supposed to randomly fall onto random winning amount. I did best I could to apply randomness into this but for some reason, each time I run multiple chip function, i always get same winning amount. Here is the big chunk of my code:
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
//If the user has chosen option "1":
void drop_multiple()
{
double total_amount = 0;
int number_of_chips = 0;
int starting_slot = -1;
int current_position = -1;
const int TOTAL_LEVELS = 13;
int shift = rand() % 2;
srand(time(0));
cout << "How many chips?: " ;
cin >> number_of_chips;
cout << endl;
if (number_of_chips < 0)
cout << number_of_chips << " is an invalid entry." << endl;
else
{
cout << "Enter slot number: " ;
cin >> starting_slot;
if (starting_slot < 0 || starting_slot > 8)
cout << "Invalid Entry." << endl;
else
{
for (int i = 0; i < number_of_chips; i++) //loop for every chip dropped in starting_slot
{
current_position = starting_slot; //resets bounce back and forth for each chip
for (int j = 0; j < TOTAL_LEVELS; j++)
{
shift = rand() % 2;
// Moves token right or left
if (shift == 0)
current_position = current_position - 0.5;
else if (shift == 1)
current_position = current_position + 0.5;
// Checks bounds
if (current_position <= 0)
current_position = 0.5;
else if (current_position >= 8)
current_position = 7.5;
} // end inner for loop
total_amount = total_amount + calc_amount(current_position); // totals and adds earnings for where the chip lands according to the calc_amount function
} // end outer for loop
} // end inner else
} // end outer else
if (total_amount <= 0)
{
cout << "\nSorry, you didn't earn any money! Please try again." << endl;
}
else
{
cout << "\nThe average of winning amount is: $" << total_amount / double (number_of_chips) << endl; //notice that this is integer division
cout << "Congrats! You have earned total: $" << total_amount << endl;
}
} // end function
Please help me figure out what the problem is... Thanks.
Last edited on Feb 7, 2013 at 3:52am Feb 7, 2013 at 3:52am UTC
Feb 7, 2013 at 9:54am Feb 7, 2013 at 9:54am UTC
remove line 11 and put it at the beginning of the main() function. It's not good to seed the random function more then once