Public Group

# Randomize

This topic is 4683 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).

1. 1
Rutin
45
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 11
• 13
• 9
• 10
• 12
• ### Forum Statistics

• Total Topics
633002
• Total Posts
3009823
• ### Who's Online (See full list)

There are no registered users currently online

×