Randomize

Started by
29 comments, last by jbadams 18 years, 3 months ago
Quote:Original post by Kazgoroth
Do please refrain from posting in all caps, it comes off as rather rude. GDKnight has attempted to help you; if you'd like more information feel free to ask, but people here give thier time for free to help you, so cut the attitude. Also, this is really a beginners topic, so I'm moving it to the appropriate forum.



Now, this sounds suspiciously like doing your homework for you, so I'm not going to just hand over sourcecode, but I will walk you through what needs to be done. If you attempt to code a solution yourself and post the code, I'm sure people will be happy to give you pointers as to how you can fix any problems.

As I understand it, you wish to create a program which simulates rolling a pair of dice a certain number of times (which will be specified by the user). Now, if we think about this it can be broken down in to a number of smaller tasks.


First, you basically want to perform the same task a (unknown) number of times; generate a pair of random numbers and display it. To me this sounds like an excellent opportunity to create and use a function (function tutorial) which you can then call as many times as you need to.

So, we take the code to generate a random number, and place it in a function. Have that function return the random number. Now you can call this function as often as you like, and get a different random number each time. To output two random numbers, simply output the results of the function twice - it will give you a different number each time. You can directly output the results of the function, you don't need to store it in a variable first (i.e. you can simply put the function call in place of a variable on your output line).


Now, you've got a function that outputs random numbers, and outputting the result twice will give you a pair of results. This will nicely cover rolling a pair of dice once. You now need to do this task a number of times, so ask the user to input this value (tutorial covering basic input/output), and store it in a variable.

Since you want to perform the same task a certain amount of times, a For Loop (tutorial covering for loops) perfectly suits your needs. You can use the variable you got from the user earlier to set how many times your loop is executed.

So, the steps you need to carry out are:
- Ask the user to input the number of rolls they want. Store this in a variable.
- Have a function which generates and returns a random number.
- Set up a for loop to execute the number of times specified by the user.
- Have an output function inside that loop which outputs a pair of random numbers.

Hope that helps. Have a go at it, and if you can't figure it out then post your code and we'll see if we can give you some tips to improve it. Now you be nice to the other posters from now on, they're trying to help you. [wink]

The tutorials listed in this post are from www.cprogramming.com, and the full set can be found here.


I honestly don't think the OP was copping an attitude.
Don't be afraid to be yourself. Nobody else ever will be.
Advertisement
Quote:Original post by Death2You
why 10 and not 6? in the %


He was just presenting an example. You do actually want to use a 6 for your proper version. bit64 has explained it fairly well, but just to clean it up so that you'll easily be able to understand:

(rand() % n); will produce a random number between 0 and n-1. Now, when you're rolling dice, you can't get a 0 obviously, so we add 1 to the value:
(rand() % n) + 1 now produces a random number between 1 and n. If you make n the number of sides on the dice (i.e. 6 for standard) you'll get a function that simulates a dice roll.

The srand(); call people have also been advising is because rand() doesn't truly produce random numbers, so if you just use it by itself your program would produce the same sequence of numbers each time it gets run. To solve this, you seed the randomizing function using srand();. Typcially the current system time or another similar value is used when doing so. You only need to do this once though, not for each call to rand().

- Jason Astle-Adams

thats what i thought. that was a great example i was just wondering why its ten and not a 6. in my head im like doesnt a dice have 6? lol anyway if u guys feel im asking too many questions, like i said give me hints not code and if i ask way too many questions, tell me to stop posting stuff and i promise i wont
im not asking for help seriosuly i sent the code to my teacher through email. she sent me an email and said i got 95 on the test. i got a question wrong not the program thank god!! anyway if u want to see the question i got wrong ill type it up. i know how to do it now b/c my teacher told me so im not asking for ur help. i just want to show u the question i got wrong. sry i know im annoying.

Q: what is the output produced by the following segment of code?
int x=12
do{
cout<<"*";
x-=3;
}while (x!=1);
cout<<"!";

i know its a simple question! i know the answer now so dont get mad that i dont know cause i do.
oh yeah the test was about the randomize and a temperature program that i did:

nsj7myx4

/*Enter Temperature*/
#include <iostream.h>

int main()
{

int Count=0;
double Temperature=0.0;
double Average=0.0;

cout<<endl;
do

{
cout<<"Enter A Temperature (999 to quit)"<<endl;
cin>>Temperature;
Average+=Temperature;
Count++;
}while (Temperature!=999);

Average/=Count;
cout<<"Average Temperature is"<<Average<<endl;

return(0);
}
thats it
Don't know what it does? Type that program into your compiler and find out.
Good to hear you did well on the test and that you managed to figure the program out.

- Jason Astle-Adams

Thanks for everyone who helped me through this rough time. i would like to wish u guys a merry christmas and a happy new year. peace
If I understand the OP, you want to know how to calculate the percentage right?
I didnt see anyone explain it, so Ill give it a try
int num_rolls = 0;int dice_results[6];memset(dice_results, 0, sizeof(*dice_results) * 6); // zero out the arrayfor(int i=0; i<10; i++) // simulate 10 rolls{  dice = 1 + rand() % 6;  dice_results[dice-1]++;  num_rolls++;}// how many percent of the rolls was a 6 dice?double pc = (double)dice_results[5] / (double)num_rolls;// pc will be the percentage as a number in the range [0, 1]cout << pc*100. << " percent of the rolls was a 6" << endl;


I have not tested this, but Im sure you get the picture.

Merry christmas to you too :D
thanks for the advice. hey maybe my teacher would of gave me extra points for doing it ur way but yeah thanks for taking time out of ur life to help me. Like i said before merry christams to everyone and wish u all a safe christmas also

This topic is closed to new replies.

Advertisement