in reply to Check randomly generated numbers have not been used before
Two approaches that might be of interest. In case you require a randomized list of unique numbers, then the problem isn't really about rand() generation, it's a list shuffling task.
With a large number of elements, various optimizations may become appropriate, such as packed vectors, mmap'ed files, etc.use List::Util q(shuffle); print join q(,), shuffle 1 .. 1000;
The second approach is to use a suitable pseudo-random number generator. One might choose a very simple linear congruential generator with a period of m. E.g. with m==224, a range and period of 16777216 is obtainable. Discard values >= 1e7 and you have a sequence of non-repeating pseudo-random numbers to cover the required 7-digit range. Keep in mind that this list will be a very weak and pseudo random sequence, suitable only in cases where the demands are casual.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Check randomly generated numbers have not been used before (shuffle)
by LanX (Saint) on Jun 21, 2014 at 01:30 UTC | |
|
Re^2: Check randomly generated numbers have not been used before (2 dimensional shuffle)
by LanX (Saint) on Jun 21, 2014 at 15:38 UTC | |
by BrowserUk (Patriarch) on Jun 22, 2014 at 09:24 UTC | |
by Anonymous Monk on Jun 22, 2014 at 13:33 UTC |