# Randomize

This topic is 4373 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys. im struggling in C++ programming hopefully someone can help me out. in this program i need to choose how many rolls i want.(rolls me in dice) if i pick 5 roll 1-5 will appear. each roll will put out two number after 5 rolls comes out there has to be a resuly of how many double there were and the percent of it is. im begging someone to make a simple program for me, or atleast tell me how to output 2 numbers in randomize instead of just one. THANKS GUYS!!!

##### Share on other sites
You can find a ton of articles of this online by doing a google search of "Random numbers in C++."

Seeing as this is such a simple thing to do here is how to generate 2 random numbers in C++.

#include <cstdlib>#include <ctime>#include <iostream>using namespace std;int main(){	// setup the varaibles.	int num1=0, num2=0;	// Setup seed based on time.	srand( (unsigned)time( 0 ) );	// create a random number between 1-10	// rand() % 10 sets max number to 10, the + 1 sets us above the 0 value.	num1 = (rand() % 10) + 1;	// repeat with second variable.	num2 = (rand() % 10) + 1;	// print the first number to the screen.	cout << num1 << endl;	// print the second number to the screen.	cout << num2 << endl;	// return 0 as exit code.	return 0;}

EDIT:

[Edited by - GDKnight on December 16, 2005 11:37:34 PM]

##### Share on other sites
GOD UR A GENIUS. HOW CAN I REPAY U
I KNOW WHAT TO DO BUT I JUST DONT KNOW HOW TO OUTPUT 2 INSTEAD OF 1 NUMBER. THANKS. OH YEAH I DID TYPE THAT IN GOOGLE BUT PEOPLE MAKE PROGRAMS WAY TO ADVANCE FOR ME

##### Share on other sites
OH YEAH BUT WHERE THE ENTER NUMBER OF ROLLS
AND WHAT IS UNSIGNED MEAN AND WHY % 10 IM SRY IM AN ASSHOLE. IM IN HIGHSCHOOL AND IM STRUGGLING WITH RANDOMIZE.

##### Share on other sites
im in high school too ;) but the % sign is like saying remainder when you did long division in fourth grade (you no, like 14/3 = 4 Remainder 2, with the remainder 2).
but the "(rand() % 10)" function will divide the number returned by "rand()" by ten and it will return the "Remainder"(the thing i was talking about up there lol). But, so it will return a number between 0 & 9, then the plus 1 will...(well you should know that part)

but before everytime you should run "srand((unsigned)time( 0 ) );" because this will make the number random, and not the same number over and over. Hope that helped

##### Share on other sites
Quote:
 Original post by Bliz23but before everytime you should run "srand((unsigned)time( 0 ) );" because this will make the number random, and not the same number over and over. Hope that helped

I think you mean: "At the start of your program, you should call srand((unsigned)time(0)). This initializes ("seeds") the random number generator, so you don't get the same sequence of numbers every time you run your program."

You shouldn't call srand every time you call rand.

Also, unsigned defines what range of numbers are possible - in this case numbers without a sign - so: {0, 1, 2, 3, 4, ...}. Whereas signed specifies: {..., -2, -1, 0, 1, 2, ...}. The default is signed (so that is what is used when neither is specified).

##### Share on other sites
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.

##### Share on other sites
Thnaks. im deeply sry for putting it in caps, and i dont want u to do my homework cause i did everything myself of the topics before randomize. i would rather be helped by people like urselves that take time out of their day to help others. I dont want u to think that i want u guys to do my homework. thats not it at all, its just i have such a rough time sticking randomize in my brain.lol anyway if i ask for help again for something and u reply, u dont have to give me any code, just hints, cause im a smart guy and ill figure it out the hard way!! Thanks guys

##### Share on other sites
why 10 and not 6? in the %

##### Share on other sites
Hey.

%10 gives you a number in the range of 0 through 9.
If you want a number in the range of 0 through 5 you would use six.

So......

//you only need to call this once
srand(timeGetTime());

int result = (rand() % N_SIDED_DICE) +1;
//so if you wanted a 4 sided die to be rolled
//you would put 4 where N_SIDED_DICE is
//that will give you a number from 0 to 3
//then you add 1 to give you a number from 1 to 4

Also...just so you know...the number generated by each call to rand() becomes the new seed for the next call to the random function (at least for the majority of pseudo-random number generators).

##### Share on other sites
Quote:

I honestly don't think the OP was copping an attitude.

##### Share on other sites
Quote:
 Original post by Death2Youwhy 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().

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
Don't know what it does? Type that program into your compiler and find out.

##### Share on other sites
Good to hear you did well on the test and that you managed to figure the program out.

##### Share on other sites
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

##### Share on other sites
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

##### Share on other sites
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

##### Share on other sites
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

##### Share on other sites
Quote:
 Original post by Death2Youoh yeah the test was about the randomize and a temperature program that i did:nsj7myx4/*Enter Temperature*/#include int main(){int Count=0;double Temperature=0.0;double Average=0.0;cout<>Temperature; Average+=Temperature;Count++;}while (Temperature!=999);Average/=Count;cout<<"Average Temperature is"<

Looks like you are adding the 999 to your average temperature, and I would suspect that you don't want to do that.

##### Share on other sites
Because "Average += Temperature;" is evaluated in the loop before Temperature gets tested against 999 to escape the loop - when the 999 is input, that's the last time the loop runs, but it still runs - so that value is treated as one of the inputs.

##### Share on other sites
Quote:
 Original post by Death2YouQ: what is the output produced by the following segment of code?int x=12do{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.

Was this code segment what was given on the test? If so, it won't compile and after the semicolon is added it will run for a very long time (1,431,655,769 iterations). The reason being that 12 -3 = 9 - 3 = 6 - 3 = 3 -3 = 0 (not 1), so it will go negative and run until it is positive and then finally end up at 1 after over 1 billion iterations.

##### Share on other sites

This topic is 4373 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.