Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Non-Repetitive Random Numbers

by AnomalousMonk (Monsignor)
on Apr 05, 2013 at 07:28 UTC ( #1027086=note: print w/ replies, xml ) Need Help??


in reply to Non-Repetitive Random Numbers

I am by no means an initiate of the Dark Mysteries™ of random numbers (Update: see reply below of BrowserUk), but here's a possibly interesting use of an iterator:

>perl -wMstrict -le "use List::Util qw(shuffle); ;; sub Iterator (&) { return $_[0]; } ;; sub rand_iter { my $total = shift; ;; my $random = [ shuffle 0 .. $total-1 ]; ;; return Iterator { my $n = shift; return if $n < 0 or $n > @$random; return splice @$random, 0, $n; } } ;; my $iter = rand_iter(9); ;; use Data::Dump; dd [ $iter->(0) ]; dd [ $iter->(5) ]; dd [ $iter->(4) ]; dd [ $iter->(1) ]; dd [ $iter->(2) ]; " [] [5, 4, 0, 2, 8] [7, 3, 6, 1] [] []


Comment on Re: Non-Repetitive Random Numbers
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1027086]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2014-08-22 21:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (165 votes), past polls