Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Non-Repetitive Random Numbers

by AnomalousMonk (Abbot)
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 imbibing at the Monastery: (8)
As of 2015-07-07 07:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (87 votes), past polls