View previous topic :: View next topic |
Author |
Message |
ManOnMoon Cheater
Reputation: 0
Joined: 17 Mar 2009 Posts: 26
|
Posted: Thu Feb 11, 2010 12:09 am Post subject: Craps Game |
|
|
Thanks everyone that posted for the help on my last program.
So I was assigned another program to design for homework, a craps game. I have all the rules set up for it except that I cant make the dice roll 2 times. Whenever I run the program I keep getting the same number.
here is what I have for it:
Code: | int rollone(int sides)
{
int r,A,B,rprime;
r=random();
A=0;
B=sides;
rprime=(int)floor(r/(RAND_MAX+1.0)*(B-A+1.0))+A;
return rprime;
}
int rolltwo(int sides)
{
int r,A,B,rprime;
r=random();
A=0;
B=sides;
rprime=(int)floor(r/(RAND_MAX+1.0)*(B-A+1.0))+A;
return rprime;
}
/* Two dice */
int main(void)
{
int sides,dice;
sides = 6;
dice = rollone(sides) + rolltwo(sides);
srandom(time(NULL));
printf("Your number is %d\n",dice);
return 0;
} |
|
|
Back to top |
|
 |
FullyAwesome I post too much
Reputation: 0
Joined: 05 Apr 2007 Posts: 4438 Location: Land Down Under
|
Posted: Thu Feb 11, 2010 12:31 am Post subject: |
|
|
the random function needs a seed to start off with, chuck in a randomize(), which uses your clock to make a seed.
_________________
|
|
Back to top |
|
 |
smartz993 I post too much
Reputation: 2
Joined: 20 Jun 2006 Posts: 2013 Location: USA
|
Posted: Thu Feb 11, 2010 12:34 am Post subject: |
|
|
Lol, how about you call srandom before calling rollone and rolltwo?
It also seems very unnecessary to have two functions that do exactly the same thing, unless that's part of your project's specifications.
|
|
Back to top |
|
 |
igoticecream Grandmaster Cheater Supreme
Reputation: 0
Joined: 23 Apr 2006 Posts: 1807 Location: 0x00400000
|
Posted: Thu Feb 11, 2010 1:06 am Post subject: |
|
|
Code: |
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
srand (time(NULL));
for(int x=0; x<10;x++)
{
int randomNumber = (rand() % 4 + 100);
cout<<randomNumber<<endl;
}
getchar();
return 0;
}
|
this will generate random numbers between 100 and 104
_________________
+~ |
|
Back to top |
|
 |
ManOnMoon Cheater
Reputation: 0
Joined: 17 Mar 2009 Posts: 26
|
Posted: Thu Feb 11, 2010 2:14 am Post subject: |
|
|
smartz993: well I have to roll two dice and he said that if I generated a number between 1-12 it would throw off the odds so I have to simulate two die and add them together
smartz993: ^^^ I cant use it except I might modify it to give me between 1-6 and make it do it two times then add it
|
|
Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Thu Feb 11, 2010 2:19 am Post subject: |
|
|
ManOnMoon wrote: | smartz993: well I have to roll two dice and he said that if I generated a number between 1-12 it would throw off the odds | He didn't mean that. You can call the same function and two have different results within range 1-6.
I'm surprised you limited the range by scaling. That's better option compared to igoticecream's modulo.
|
|
Back to top |
|
 |
ManOnMoon Cheater
Reputation: 0
Joined: 17 Mar 2009 Posts: 26
|
Posted: Thu Feb 11, 2010 9:51 pm Post subject: |
|
|
Here is what I changed my new rolling function in order to get two dice. Except every so often I get 0 which is impossible when you roll two dice haha. So I was wondering how do I go about making sure its greater than 0.
Code: |
int pairofdice()
{
int rollone, rolltwo, totalroll;
rollone=(int)floor(random()/(RAND_MAX+1.0)*(6-1+1.0))+1;
rolltwo=(int)floor(random()/(RAND_MAX+1.0)*(6-1+1.0))+1;
totalroll= rollone +rolltwo;
return totalroll;
}
int main(){
srandom(time(NULL));
printf ("Your roll %d \n",pairofdice());
return 0;
} |
Never mind I just figured out what was wrong. Now I am moving on to setting the game rules. Except how do I stop the game from stopping and make it continue until you have 0 money from the start.
|
|
Back to top |
|
 |
igoticecream Grandmaster Cheater Supreme
Reputation: 0
Joined: 23 Apr 2006 Posts: 1807 Location: 0x00400000
|
Posted: Thu Feb 11, 2010 10:45 pm Post subject: |
|
|
ManOnMoon wrote: | Here is what I changed my new rolling function in order to get two dice. Except every so often I get 0 which is impossible when you roll two dice haha. So I was wondering how do I go about making sure its greater than 0.
Code: |
int pairofdice()
{
int rollone, rolltwo, totalroll;
rollone=(int)floor(random()/(RAND_MAX+1.0)*(6-1+1.0))+1;
rolltwo=(int)floor(random()/(RAND_MAX+1.0)*(6-1+1.0))+1;
totalroll= rollone +rolltwo;
return totalroll;
}
int main(){
srandom(time(NULL));
printf ("Your roll %d \n",pairofdice());
return 0;
} |
Never mind I just figured out what was wrong. Now I am moving on to setting the game rules. Except how do I stop the game from stopping and make it continue until you have 0 money from the start. |
You just copy+paste all the time without understanding what are you pasting? check out the code i posted, i have set a random limit between 100 ~ 104... do the same but 1~6 (is a dice right? 6 faces)
_________________
+~ |
|
Back to top |
|
 |
ManOnMoon Cheater
Reputation: 0
Joined: 17 Mar 2009 Posts: 26
|
Posted: Fri Feb 12, 2010 1:59 am Post subject: |
|
|
Actually that code is a modified version of the single die program I made a week or so ago with my roommate where it asked how many sides you wanted on a die. Except I modified it to replace the variables with numbers. I looked at what you gave me but I didn't know how to incorporate it. Into the game it wasn't until earlier today that I looked at it again and saw how to set the parameters for what you posted but I don't know how to apply it.
|
|
Back to top |
|
 |
|