# Thread: Random Numbers

1. ## Random Numbers

Hi.

Before you say anything, I have read the FAQ, but I am very new to coding (atleast C++) and it didn't help much.

How do I make a random number, say.... a number from 1-5??

2. 2

there you go.

wait

5

no no.

1

feel free to use me as your random number generator anytime.

3. seriously.
srand(a_number) function will seed your number generator.
then use rand() anytime you need a random number.

to get it between two numbers try this: I pulled it from a book but have not tried it.

random_number = (rand() % (max + 1 - min) + min);

4. it works.

5. but this gives the same "random" number each time the program is executed. How is it done so it gives a new number each time?

6. for a start:
srand( (unsigned)time(0) );

that uses time as you're "random" seed thus making the seed number different each time. If you need truly random numbers you'll have to read up on the topic.

7. Make sure you use srand to seed the rand function. srand is only called once (don't include it in a loop, for example). Rand() can be in a loop though. You can enter a number for srand, but that'll result in the same random numbers if the same number is entered. Seed with the time function in <ctime>.
Code:
```srand(time(0));
.
.
.
x = y + rand() % z;```
y is the number you want to start the random numbers with, and z is the range of numbers you want them to fall into. E.g., To write one dice being cast:
Code:
`num = 1 + rand() % 6;`
The die starts at one, and can be any of six numbers, 1 thru 6.

8. num = 1 + rand() % 6;

9. num = 1 + rand() % 6;
If you're new to coding, it's very important that you understand how this statement works. The % in the above code line is referred to as the modulus operator. The modulus operator gives what is more coloquially known as the remainder of a division. rand() supplies a random number between 0 and 32767 or something similar (it varies by compiler). When you take that number and modulus by 6, you get a remainder, which must be a number smaller than 6. Example:

1000 % 6 = 4
because:
1000 / 6 = 996 R 4

This will give you a range between 0 and 5. To make it 1 through 6 we add 1 to the equation.

10. This is my code...
Code:
```#include <iostream.h>

srand();

int main()
{
int number;
number=1+rand()%6;
cout<<number;
return 0;
}```
That always gets the number 5... How do I fix that??

11. See the above posts, you need to seed the rand function.

#include <ctime>

//In main - you can't call functions globally

srand( (unsigned)time(0) );

12. Yay!! It works.

Thank-you all for your help, I'm very new to C++.

13. Also, if you want your random number to be between 1 and 5, per your first post, change ...rand() % 6 to ...rand() % 5.

14. You people are so gay, here goes the easy way.

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <iostream.h>

void main()
{
clrscr();
randomize(); //initiates the random function
int x=random(5); // generates a random number b/w 1-5
cout << x;
getch();
}

Popular pages Recent additions