Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Yes, that works. And that method is great if you want to grab multiple random numbers such that you never get the same one twice. For example, implementing a shuffled deck of cards, or drawing names out of a hat. But if all the OP wants is a single random number, or lots of random numbers uninhibited by such rules as "the number can never repeat", that solution is probably not the best way to do it.

With a shuffled list you have the following problems:

  • Each number can only exist once, or at least a predetermined finite number of times. True random numbers are not dependant on what has already been picked. If you want only four aces to exist in a deck, your solution is great. If you want each subsequent draw to be from a 'fresh' deck, your method would require a new deck, and a new shuffle after each draw.
  • The actions of building the deck (the list), shuffling it, and copying it to @num each take O(n) time.
  • For any range of randoms, your 'deck' grows in memory requirements in a 1:1 relationship ( O(n) ). Thus, a huge range will require huge amounts of memory.

On the other hand, simply picking a random number using rand insures that each subsequent random number isn't dependant on the previously selected numbers, no lists are built and maintained, and thus, time and memory requirements are only O(1).


In reply to Re^2: random number with range by davido
in thread random number with range by Anonymous Monk

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (3)
    As of 2018-02-23 05:44 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (300 votes). Check out past polls.