Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Non-Repetitive Random Numbers

by AnomalousMonk (Chancellor)
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] [] []

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1027086]
help
Chatterbox?
[ovedpo15]: If I use "unless(opendir(my $dir, $test_dir))" why does it give me error "Global symbol "$dir" requires explicit package name at" if I used "my"? If I use my $dir before the opendir it works.. but why?

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2018-04-22 15:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?