Help getting dice to roll 10000 times.
Oct 1, 2013 at 2:31am UTC
I have to write a program that rolls 2 dice 10000 times in a game of craps and calculate the odds of winning and losing.
I have the program working to 1 roll but honestly i cant figure out how to get it to roll multiple times.
Thanks! Heres the code so far:
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
#include <iostream>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int diceroll(); //funtion reference
//global variables
int dice1;
int dice2;
int rollSum;
int sum;
int rollPoint;
double oddslosing;
double odds;
string status;
int w = 0; //wins
int l = 0; //lose
int main()
{
sum = diceroll();
switch (sum) //switch for numbers
{
case 2:
case 3:
case 12:
status = "lose" ;
++l;
break ;
case 7:
case 11:
status = "win" ;
++w;
break ;
default :
status = "point" ;
rollPoint = sum;
break ;
}
while (status == "point" ) //point loop
{
sum = diceroll();
if (sum == rollPoint)
status = "win" ;
else if (sum == 7)
status = "lose" ;
else
status = "point" ;
}
if (status == "win" )
++w;
if (status == "lose" )
++l;
odds = ((double )w/(w+l));
oddslosing = ((double )l/(w+l));
cout<< "odds of winning: " <<odds<<endl;
cout<< "odds of losing: " <<oddslosing<<endl;
return 0;
}
int diceroll() //function for rolldice
{
dice1 = (rand() + time(0)) % 6 + 1;
dice2 = (rand() + time(0)) % 6 + 1;
rollSum = (dice1 + dice2);
return rollSum;
}
Last edited on Oct 1, 2013 at 2:59am UTC
Oct 1, 2013 at 3:04am UTC
like this perhaps?
i ran it 10 million times for kicks and got
win: .535538
lose: .464462
neat program =)
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
#include <iostream>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int diceroll(); //funtion reference
//global variables
int dice1;
int dice2;
int rollSum;
int sum;
int rollPoint;
double oddslosing;
double odds;
string status;
int w = 0; //wins
int l = 0; //lose
int main() {
for (int i=0; i<10000; ++i) {
sum = diceroll();
switch (sum) //switch for numbers
{
case 2:
case 3:
case 12:
status = "lose" ;
++l;
break ;
case 7:
case 11:
status = "win" ;
++w;
break ;
default :
status = "point" ;
rollPoint = sum;
break ;
}
while (status == "point" ) //point loop
{
sum = diceroll();
if (sum == rollPoint)
status = "win" ;
else if (sum == 7)
status = "lose" ;
else
status = "point" ;
}
if (status == "win" )
++w;
if (status == "lose" )
++l;
}
odds = ((double )w/(w+l));
oddslosing = ((double )l/(w+l));
cout<< "odds of winning: " <<odds<<endl;
cout<< "odds of losing: " <<oddslosing<<endl;
cin.ignore();
return 0;
}
int diceroll() { //function for rolldice
dice1 = (rand() + time(0)) % 6 + 1;
dice2 = (rand() + time(0)) % 6 + 1;
rollSum = (dice1 + dice2);
return rollSum;
}
Last edited on Oct 1, 2013 at 3:10am UTC
Oct 1, 2013 at 3:14am UTC
Perfect! Much appreciated!
Thanks! :D
Topic archived. No new replies allowed.