random number
-
Can someone pls provide me with an algorithim that generates an random number ( 0 to 1000)and it cannot generate the same random number more than once.
The output wouldn't be a random number as you are affecting the outcome, however to generate random numbers: // seed the random number generator using the current time srand((unsigned)time(NULL)); // gets a random number between 0 and 0x7fff int iRandom = rand(); // scale this number down to a value between 0 and 1000. iRandom /= (RAND_MAX / 1000); Regards, Simon
-
The output wouldn't be a random number as you are affecting the outcome, however to generate random numbers: // seed the random number generator using the current time srand((unsigned)time(NULL)); // gets a random number between 0 and 0x7fff int iRandom = rand(); // scale this number down to a value between 0 and 1000. iRandom /= (RAND_MAX / 1000); Regards, Simon
thanks for your replay Mr.simon but with // seed the random number generator using the current time srand((unsigned)time(NULL)); the number will be repeated, but for me numbers shouldn't repeated in same sequence .
-
thanks for your replay Mr.simon but with // seed the random number generator using the current time srand((unsigned)time(NULL)); the number will be repeated, but for me numbers shouldn't repeated in same sequence .
You could hold all generated random numbers in a vector and check that the next number has not been used previously. If it has call the random again. As long as you only want a few, i.e small percentage of the possible 1000 pseudo random numbers in your sequence, this will suffice. Ant.
-
thanks for your replay Mr.simon but with // seed the random number generator using the current time srand((unsigned)time(NULL)); the number will be repeated, but for me numbers shouldn't repeated in same sequence .
The numbers will be repeated in the same sequence only if you start two copies of the application at the same time - the numbers are psuedorandom. If that isn't good enough then you could seed the timer again each time you get a new random number - then differences such as the processor speed / load etc will mean that the sequence will definitely not be repeated. Regards, Simon
-
Can someone pls provide me with an algorithim that generates an random number ( 0 to 1000)and it cannot generate the same random number more than once.
class IncInt { private : int i_ ; public : IncInt ( int i ) : i_ ( i ) {} int operator () () { return i_++ ; } } ; void Random1000 () { std::vector vi ; vi.resize ( 1000 ) ; std::generate ( vi.begin (), vi.end (), IncInt ( 0 )) ; std::random_shuffle ( vi.begin (), vi.end ()) ; // do something with randomised sequence... }
Gives you vector containing 0-999 in random order. You can use a custom 'random' function as an extra argument to 'random_shuffle' if you wish. I'll leave it up to you to decide how to wrap this up, should it suit your purpose. Paul -
thanks for your replay Mr.simon but with // seed the random number generator using the current time srand((unsigned)time(NULL)); the number will be repeated, but for me numbers shouldn't repeated in same sequence .
The return value of
time()
is the number of seconds since January 1, 1970 00:00 (or December 31, 1969 19:00). In any case, as long as its not called two successive times in less than one second, the return value will always be different sosrand()
will always be seeded uniquely.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
The numbers will be repeated in the same sequence only if you start two copies of the application at the same time - the numbers are psuedorandom. If that isn't good enough then you could seed the timer again each time you get a new random number - then differences such as the processor speed / load etc will mean that the sequence will definitely not be repeated. Regards, Simon
Seeding the random number generator each time is a VERY BAD IDEA. For example, if you can generate 10,000 random numbers in a second, you will get the same random number each time since time only has a resolution of a second. Also, many random number generators should be initialized with a large prime number. By not doing that, it can take a few interation before the random numbers start getting "good". Reseeding the random number generator constantly places it back in the initial condition of generating a small series of bad values. Tim Smith I'm going to patent thought. I have yet to see any prior art.
-
class IncInt { private : int i_ ; public : IncInt ( int i ) : i_ ( i ) {} int operator () () { return i_++ ; } } ; void Random1000 () { std::vector vi ; vi.resize ( 1000 ) ; std::generate ( vi.begin (), vi.end (), IncInt ( 0 )) ; std::random_shuffle ( vi.begin (), vi.end ()) ; // do something with randomised sequence... }
Gives you vector containing 0-999 in random order. You can use a custom 'random' function as an extra argument to 'random_shuffle' if you wish. I'll leave it up to you to decide how to wrap this up, should it suit your purpose. Paulor
void Random1000 ()
{
std::vector vi ;
for (int i = 0; i < 1000; i++)
vi .push_back (i);
std::random_shuffle ( vi.begin (), vi.end ()) ;
// do something with randomised sequence...
}If you don't want all that STL obfuscation in there. Why write 15 lines of code when 2 works just fine. Tim Smith I'm going to patent thought. I have yet to see any prior art.
-
or
void Random1000 ()
{
std::vector vi ;
for (int i = 0; i < 1000; i++)
vi .push_back (i);
std::random_shuffle ( vi.begin (), vi.end ()) ;
// do something with randomised sequence...
}If you don't want all that STL obfuscation in there. Why write 15 lines of code when 2 works just fine. Tim Smith I'm going to patent thought. I have yet to see any prior art.
I only had to write one line.... But it was necessary to cut and paste a bit to make it make sense here. Anyway it's the ease of using the random_shuffle that's important. The OP didn't want a sequence of random numbers, AFAICT. It could of course be written,
void Random1000() { int vi [ 1000 ] ; for ( int i = 0; i < 1000; ++i ) vi [ i ] = i ; std::random_shuffle ( vi, vi + 1000 ) ; ... }
Paul -
Seeding the random number generator each time is a VERY BAD IDEA. For example, if you can generate 10,000 random numbers in a second, you will get the same random number each time since time only has a resolution of a second. Also, many random number generators should be initialized with a large prime number. By not doing that, it can take a few interation before the random numbers start getting "good". Reseeding the random number generator constantly places it back in the initial condition of generating a small series of bad values. Tim Smith I'm going to patent thought. I have yet to see any prior art.