1. ## simple question about function/random #'s

I'm creating a program that has a function that rolls a die to create a random number between 1-6. I have that figured out and running. The problem is that I need to call the function several times during the program, each with an individual result (randomly generated doubles are acceptable, though). These numbers need to be compared, too. The function is caleld up to 20 times, and i don't want to create 20 different variables within my main, all equal to the function output. Is there any way that I am able to do this? 2. Arrays, my friend.

int my_random_numbers; 3. being the c++ newbie that I am, i don't understand how to use that. Also, the function is being called at dirrent times during the program, if that is of any relevance.

-->edit: what i am going to expieriment with is using two functions, and just calling the both of them and comparing those (two dice are being rolled in the game, and when i call the function w/o assigning it to a local variable, the results are random) Is the function a function you made yourself, or is it "rand()"?

The problem is that I need to call the function several times during the program, each with an individual result
I don't see the problem here... each time you get a random number, it gives you a different random number. Otherwise it wouldn't be very random...
(randomly generated doubles are acceptable, though)
Wha??? Isn't that the same thing?? The function is caleld up to 20 times, and i don't want to create 20 different variables within my main
Hmm, I think there's a misunderstanding... you can reuse variables.
Code:
```#include <time.h>
#include <iostream>

int main()
{
srand(time(NULL));

//Create 2 variables to hold the random numbers
int randomNumber1, randomNumber2;

for(int i = 0; i < 20; ++i)    //Loop 20 times
{
randomNumber1 = (rand() % 6) + 1;
randomNumber2 = (rand() % 6) + 1;

if(randomNumber1 == randomNumber2)
std::cout << "Equal." << std::endl;
else
std::cout << "Not equal." << std::endl;
}
return 0;
}```
I'm not 100% sure if the srand() line will work, I'm used to using GetTickCount() instead of time(NULL) since I do more Windows programs, but other than that line, the code should generate 20 pairs of random numbers and compare them for equality then quit.

**EDIT** In case you meant you need to generate 20 random numbers and store ALL of them, then if you don't know how to use arrays, I suggest you learn to. In fact, I suggest you learn to use them regardless  5. I'll go more in-depth into the specifics of what my program is meant to do.

-There are two players, and there are two dice.

-player one, the user, rolls die 1. he them guesses if die 2 will get higher/lower/same as die 1. he rolls die #2, and an appropriate score is given

-player 2, the AI, rolls die 1, and predicts what die 2 will be based on statistics entered by me (when i get ot that). die 2 is rolled and appropriate score is given.

-for each correct guess, +1 point is given, and for each wrong guess, -1 point is given. the first to 10 wins. I have it working if the functions are only called once, because i assign them each to a local variable, and compare those. my problem with comparing the functions is that they always change, meaning the numbers put up on screen are not the one being compared by the computer (since a function is called once to display and a second time to compare).

I hope you see now why I say it could gho up to 20, or even more, since each player needs to get 10 points to win.

Wha??? Isn't that the same thing??
What i meant in the first statement is not having all values as "1" or "2", etc. in the second statement, i was talking about numbers that appear as doubles randomly, which will happen, since more than 6 numbers are being generated. 6. Looks like you already understand the problem then But if you look at the code I gave you, you should be able to see the solution to your problem. Just do a loop until either player or computer has a score of 20, in which you store a random number in a variable, display the variable, store another random number, compare the 2, assign points, etc.

I hope you've learned loops already?? 7. Yes, i know how to use loops .

i'll try what you suggested, this is greatly appreciated Popular pages Recent additions 