1. Promlems with Random Numbers

I am new to programming C++, so I am trying to make a simple Slot Machine Program. Everything works fine, except the random numbers. Every time I run the program, I get the same three "random" numbers! Here is the code I am using for my random numbers:

int x=rand()%21;
int y=rand()%21;
int z=rand()%21;

The numbers I keep getting are 20, 8, and 13. The random numbers I want to get is the range of 1 to 20.

Thank you all for your help!

2. Oops

It should be: Problems with Random Numbers

3. Check the FAQ

4. It could also be "problem with the FAQ"

You should only seed with srand() once.

5. Ummm

What do you mean seed only once?

6. A seed initialises a series of random numbers. If you are re-seeding with the same number, you will get the same series.

7. Ok...

So what should my code look like?

The FAQ just confused me even more...

8. Did you try any of the examples in the FAQ, using iostream the second one is -

Code:
```#include <cstdlib> //for rand
#include <iostream> //for cout
#include <ctime>   //for time

using namespace std;

int main(void)
{
srand(time(NULL));
cout << "A random number from 0 to 99: %d"<< rand() % 100;
return 0;
}```
What's confusing?

9. ok not that i like the fact that you didnt check the FAQ to begin with, but this isnt really in the faq, it just takes a little thinking, here is a sample of what your code should look like, i commented almost everything to show you what is happening thoughout the program.

Code:
```#include <time.h>
#include <stdlib.h>
#include <iostream.h>

int main()
{

int i, j, k; //some variables
srand(time(NULL)); //sets a random seed for a random seed.
//this will allow us to get a random number 3 times
k=rand() %1000; //sets a number between 0 and 1000. (used later)

for(i=0; i<3; i++){ //simple for statment notice that 'k' is not included here
//although it could be, its included at the bottom. also
//notice that k gets increased by one each time.
srand(k);         //this sets the random seed for our numbers. sence
//k it self is a random number, when the program is run
//more than once it will be changed every time and
//sence k gets higher by 1 each time, the seed is
//never the same.

j=rand() % 100;  //now that we have our truly random seed we can
//make random numbers this will give a number between
//0-100.
cout<<j<<endl;
k++;                  //k gets increased by one for the next time its used for
//a random seed
}
}```
if k wernt a random number it self k would end up being 1,2, or 3 and spit out the same numbers. sence it got increased by 1 each time. sence k IS a random number and has a fairly high field (0-1000), it will be random number such as 592 then the seed would change to 1+ the random number making it 593 then add another 1 to it making it 594 and so on.

10. Jesus, is it any wonder people are confused. There's no reason to keep calling srand(). When a program is run more than once it'll be run at a different time, making your k redundant.

11. Sorensen is right.

Only call srand() once!

Look at Sorensen's example, I couldn't have done better myself.

12. rkjd2 >> Thats what is called a kludge. A work around that technicaly works but it isn't a pretty solution.

13. ok first he wasnt asking for a SINGLE random number. there for he needs 3 random numbers, if i calll srand one time, rand will spit out the same number each time (trust me i've done this before). if it's put in a loop it dosnt matter what the time variable is sence the time stays the same thoughout the entire program (its only inilized once). this is actually pretty simple solution and it works well.

14. besides u have to use a variable for srand otherwise you couldnt manipulate it. and if you used a variable with 1,2,3 as values, you would be calling the same seeds getting the same numbers. there for you must have a random seed to give k a random number.