in reply to Idiom to return 0 or random number of array elements
I can then extend that to return a random number of elements: @array[0 .. int rand @array];. But I need to also sometimes get 0 elements.
That will return 0 elements approx. 10% of the time:
@array = 0 .. 9;; sub x{ return @array[ 0 .. int( rand @array ) ]; };; $stats{ scalar( x() ) }++ for 1 .. 1000;; pp \%stats;; { "0" => 90, 1 => 103, 2 => 126, 3 => 99, 4 => 101, 5 => 101, 6 => 88, + 7 => 97, 8 => 105, 9 => 90 }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Idiom to return 0 or random number of array elements
by LanX (Saint) on Dec 07, 2012 at 05:46 UTC | |
|
Re^2: Idiom to return 0 or random number of array elements
by eyepopslikeamosquito (Archbishop) on Dec 07, 2012 at 10:31 UTC | |
by tobyink (Canon) on Dec 07, 2012 at 12:29 UTC | |
|
Re^2: Idiom to return 0 or random number of array elements
by Anonymous Monk on Dec 07, 2012 at 05:47 UTC | |
|
Re^2: Idiom to return 0 or random number of array elements
by Anonymous Monk on Dec 07, 2012 at 15:36 UTC | |
by Anonymous Monk on Dec 07, 2012 at 16:01 UTC |