#!perl -w @nums = 100000 .. 999999; # Generate all the possible numbers ## Shuffle them (use a better rand if needed) $_ ne ($a=int( rand( $#nums-$_+1))+$_) and $nums[$_] ^= $nums[$a] ^= $ +nums[$_] ^= $nums[$a] for (0..$#nums); print pop@nums; # pop a new one each time you need it

The array takes about 50 seconds to initialise and shuffle (on my quite whimpy box) but much less storage than using a hash of the same size.

There are less obfu versions of the Fisher-Yates shuffle around too, but this one was handy.

Update: Having read your later posts regarding using the numbers during different runs of the program, I realise you need to save the array somewhere.

Not having made any use of the available persistant storage mechanisms that perl provides, I'd probably do something like:

#!perl -w @nums = 100000 .. 999999; # Generate all the possible numbers ## Shuffle them (use a better rand if needed) $_ ne ($a=int( rand( $#nums-$_+1))+$_) and $nums[$_] ^= $nums[$a] ^= $ +nums[$_] ^= $nums[$a] for (0..$#nums); open OUT, '>randoms' or die $!; print OUT pack 'I*', @numbs; close OUT or warn $!;

And then whenI needed one of them

.... open RNDNUM, '<randoms' or die $!; binmode(RNDNUMS); # If needed! seek RNDNUMS, -4, 2; my $newlen = tell RNDNUM; my $rndnum = do { local $/=\4; unpack 'I',<RNDNUM>; } truncate RNDNUM, $newlen or die $!; close RNDNUM or warn $!; ..... # do whatever with $rndnum

... but that's cos I know how to make this work. The second bit of code is untested, but demos the basic mechanism of pre-generating your random numbers and storing them somewhere, and then throwing them away once you've used them.

Whatever you do, don't generate a random number and then try to verify whether you've used it before. That's a recipe for disaster wasteful.

It will seem ok at first, but once your halfway through your range, you are going to (statistically) have to generate 450,000 random numbers before you find one that you haven't used before!

(I just KNOW a mathematician is gonna correct that last bit:)

Updated: removed falacious Infinite Improbability Drivel* cos the mathematician did!

With apologies to Douglas Adams RIP


What's this about a "crooked mitre"? I'm good at woodwork!

In reply to Re: Generating random 6 digit numbers by BrowserUk
in thread Generating random 6 digit numbers by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.