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??
This is a discussion on Random Numbers within the C++ Programming forums, part of the General Programming Boards category; Hi. Before you say anything, I have read the FAQ, but I am very new to coding (atleast C++) and ...
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
there you go.
wait
5
no no.
1
feel free to use me as your random number generator anytime.
If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?
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);
Last edited by taylorguitarman; 09-17-2001 at 12:43 AM.
If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?
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?
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.
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>.
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:srand(time(0)); . . . x = y + rand() % z;
The die starts at one, and can be any of six numbers, 1 thru 6.Code:num = 1 + rand() % 6;
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:num = 1 + rand() % 6;
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.
This is my code...
That always gets the number 5... How do I fix that??Code:#include <iostream.h> srand(); int main() { int number; number=1+rand()%6; cout<<number; return 0; }
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) );
Yay!! It works.
Thank-you all for your help, I'm very new to C++.
Also, if you want your random number to be between 1 and 5, per your first post, change ...rand() % 6 to ...rand() % 5.
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();
}