### a random number specific

Hey guys,

I wanted to create a code that will generate a random number,
but I would like it if it would generate a different number from 1-6 each time.

this is what I began with:

cout << rand() %6 << endl;

now I read somewhere srand could be used, but that would be from RAND_MAX,

is there a way for a random number generator that would create a different number each time? thats out from a specific range?

many thanks!
Not so sure what you mean, but if your getting the same numbers, it because you're not seeding the generator.

 ``12`` ``````#include srand(time(NULL));``````
Hey Callum,
Although I think you got what I was trying to say, allow me to be more specific...

I build and run the given code:

cout << rand() %6 << endl; //generates number from 1-6

and lets say I get 5.

After I close the program.
I build and run the program again and receive 5 again. and over and over.

I was wondering, if there is a way...where user's can get different random numbers each time. So the first time I might've gotten 5, but the next time I could get a 2 or a 4....

I understand that for the program the 5 might be a randomly generated number, but when its already a set, randomly generated number...it defeats the purpose.

I tried the given code you gave above....:

#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <ctime>
int main ()
{
int numone;

srand(time(NULL));
cout << time << endl;
return 0;
}

BUT....again...I am simply getting the same number over and over again...
any other suggestions?

@taku nishino, the code you posted above doesn't use any random numbers:
 ``12`` ``````srand(time(NULL)); cout << time << endl;``````

But if we return to the earlier code, this is what you need:
 ``123456789101112`` ``````#include #include #include using namespace std; int main () { srand(time(NULL)); cout << rand()%6 << endl; //generates number from 0-5 return 0; }``````
What Chervil said it correct,

srand() seeds the random numbers, so when you use rand() it will give a different number.
ahhhhh I get it now!
thank you to both of you! Learning things everyday! :D
Topic archived. No new replies allowed.