Ask your own question, for FREE!
Computer Science 14 Online
OpenStudy (roadjester):

I am doing a programming project that involves generating random numbers. My professor has stated that he will provide the actual random number generating function but we are to write a dice game program. A person begins with a balance of $100. From there, a person can either choose to place a bet, roll, or quit unless your balance hits zero at which point the game ends. I have a pre-read-while-loop going that will wait until I cin>> a command by use of a switch statement. If the two numbers generated are equal, I add the double of what was bet to the balance. If the sum of the two numbers is

OpenStudy (roadjester):

7, I triple the bet and add it to the balance. However, without the actual number generating function, I'm not sure how to proceed.

OpenStudy (anonymous):

So, your problem is that you don't have a pseudo random number generator? Are you writing actual code? If so (and due to your reference to cin, I assume it would be in C++), you can always use the rand() function. Check http://www.cplusplus.com/reference/clibrary/cstdlib/rand/ for reference and usage. If you are only writing pseudo-code, just write random() for the function call. The implementation details for a (unbiased) pseudo random number generator are quite nasty.

OpenStudy (roadjester):

@bmp well, my professor said he would provide the random function generator and for now to just use stubs and such to fill in the functions. But without a number generator that restricts it to 1-6, I don't know if my program is compiling/executing correctly and whether or not I have bugs.

OpenStudy (anonymous):

For testing purposes, you should be able to use rand() % 6, that would generate a random number of 0-5. If you actually need 1-6, just do (rand() % 6) + 1. That is not entirely unbiased(it's slightly biased towards the small end, such as 1 and 2), but it should suffice for what you are intending. I am assuming you have a strong understanding of the usual operators in C++. In case you don't, I will try to explain more rand() % 6. rand() is a function that will return a random number in the range [0, RAND_MAX). The mod (%) operator will return the remainder of the integer division, so, 5%2 should return 1. rand() % 6 can only return, then, numbers in range [0, 6). If it's evenly divided by 6, it should return 0, and if it's greater than or equal to 6, it's still divisible by 6. Anyway, you should test with rand (which is quite reliable) and wait for your professor to come up with the function.

OpenStudy (roadjester):

I'm going to try and show you the program, I hope it actually works...

OpenStudy (roadjester):

#include <iostream> #include <cstdlib> //***************************************************** //Project: Dice Game //Author: //Purpose: //Notes: 9/28/12 //****************************************************** using namespace std; double placeBet(double balance, double bet); //diceFunctionCall; int main() { double balance=100; double bet=10; int roll1=0; int roll2=0; char command=' '; cout<<"You begin with a balance of $100 and may choose from one of"<<" " <<"three commands:"<<endl <<" B to bet."<<endl<<" R to roll."<<endl<<" Q to quit."<<endl<<endl; cout<<"Balance: "<<balance<<endl<<endl; cout<<"Select a command. The default bet is $10."<<endl<<endl; cin>>command; while (balance>=0) { cout<<"Select a command."; cin>>command; switch (command) { case 'B': case 'b': bet=placeBet(balance,bet); break; case 'R': case 'r': roll1=(rand()%6+1); cout<<roll1<<endl<<endl; roll2=(rand()%6+1); cout<<roll2<<endl<<endl; break; case 'Q': case 'q': break; default: cout<<"Command must be B, R, or Q."<<endl; } } system("PAUSE"); return 0; } double posDouble (string prompt) { double number; cout<<prompt; cin>>number; cout<<endl; while (number<=0) { cout<<"Enter a positive number."<<endl; cout<<prompt; cin>>number; cout<<endl; } return number; } double placeBet(double balance, double bet) { balance=balance-bet; bet=posDouble("Bet: $"); while (bet>balance) { bet=posDouble("Bet: $"); } cout<<balance<<endl; return bet; }

OpenStudy (anonymous):

Sure thing, but I am not actually that proficient in C++. I can try to help, as I know a bit of C, but I am not that familiar with some language quirks.

OpenStudy (anonymous):

Looks fine (I am still configurating my windows environment to test it), but it looks like you forgot to add the srand. You see, random functions aren't truly random. They have to use something that's called a seed to a pseudo random generator (PRG). But inputing a seed to it, the program will calculate a not-so random number, called pseudo-random number. If the PRG is safe (in a sense that I will not talk about here, since it's out of the scope), then in our perspective (client-side) the numbers look random. As you are implementing, you need to define a good seed. Try out srand(1); and run the program multiple times. You will notice that even if the output looks random once, every other run will have the same output, i.e., the output depends on the seed. In order to have a (more or less) random number generator, we have to continuously change the seed. That's why in the ref I linked you, we had the srand( time(NULL) ); call. time(NULL) returns the current clock (CPU) time, so it's changing every time (it changes on every clock of the CPU). Then, I think it should work fine. I will test it more in a bit.

OpenStudy (roadjester):

The purpose of this program is basically a dice/gambling game. In my text, using the ctime library and using time will cause the time to be in seconds begging from Jan 1st 1970. However, as a game of dice, I only want 1-6 for two dice. If I get the same value for one roll, the double of my bet is added to my balance. If I roll a sum of 7, I triple my bet and add it to the balance. However, as you said, rand and srand are not completely random. There is a certain pattern to the numbers. However, I'm having difficulty writing the functions for roll and quit. Bet is relatively simple in the sense that I just subtract that from the balance, but after that, I want to roll. I'm not sure how to call the functions I need for the three cases.

OpenStudy (roadjester):

And what should be the easiest, the quit, I not sure what to put in there to END the program.

OpenStudy (roadjester):

oops, I meant "I'm not sure how to WRITE the functions I need for the three cases. I know how to call them.

OpenStudy (anonymous):

One thing is that the rest of the program so far seems to not be working okay. Might want to check that. Bear in mind one thing: computers can't be really random. They are, after all, deterministic. The 'contract' is that for an outsider (a person playing your game), your program should be the same as a truly random. And that rand + srand do achieve. So you are just fine using it. A possibility to code it is more or less like this: http://codepad.org/hZmh44Vi

OpenStudy (roadjester):

Ok, no idea how you did that but that's cool. However, I assume that some of the code you used was C code?

OpenStudy (roadjester):

My reason for asking is because I don't recognize some of the syntax.

OpenStudy (anonymous):

Nope, it's valid C++. What did you not understand? And the correct version, as far as my eye go, should be something like: http://codepad.org/D7wUGzXp

OpenStudy (roadjester):

well, first, I can just add or subtract from the balance using balance=balance+ bet*2 (for rolling a pair) and balance=balance+balance*3 (for the sum) can't I? I also have to keep track of the number of rolls made but that part I think is simple enough, initialize a variable, and every time "R" or "r" is chosen, increment that variable. would I need an entire function just to change my balance? or is that just for the purpose of writing it as a function? I've never used a boolean expression where the number came first in the == , only for inequalities. That "option" part is unclear to me

OpenStudy (anonymous):

As a general rule of thumb, if it's repeated code and you can write a function, write it. Your main loop should ideally be only function calls and initialization. Code is made for people to read. Putting a lot of stuff on the main loop is not the greatest idea. This is called abstraction. You abstract away the implementation details, giving a broader sense of what the program does. For your second point, it's just an option. You could pass both rolls as arguments and check inside the function. It's up to you to write it however you think it's clearer. That, really, is a matter of style. For the third part, it's just good habit to write 1 == x, rather than x == 1. The reason is that if you write x = 1, the program will compile and run just fine, and this is a VERY, VERY hard bug to find. On the other hand, if you write 1 = x, the compiler raises an error and you see that you forgot the extra = quite easily.

OpenStudy (roadjester):

Hmm all good points. Thanks for all of your time! If I have any more questions I'll message you, but for now, BEDTIME! :)

OpenStudy (anonymous):

No problem. Message me if you need anything. I will call it a night too. See you, and good luck with your assignment.

OpenStudy (roadjester):

Thanks, and I appreciate all of the help and time you spent. I'm extremely grateful.

OpenStudy (anonymous):

No problem, really. Had to code a bit too, I was getting rusty. Anyway, good night.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!