One of n lines in a file: Difference between revisions
Content added Content deleted
(New draft task and Python solution.) |
(→{{header|Perl 6}}: add) |
||
Line 20: | Line 20: | ||
Note: You may choose a smaller number of repetitions if necessary, but mention this up-front. |
Note: You may choose a smaller number of repetitions if necessary, but mention this up-front. |
||
=={{header|Perl 6}}== |
|||
{{trans|Python}} |
|||
<lang perl6>sub one_of_n($n) { |
|||
my $choice; |
|||
$choice = $_ if rand * $_ < 1 for 1 .. $n; |
|||
$choice - 1; |
|||
} |
|||
sub one_of_n_test($n = 10, $trials = 1000000) { |
|||
my @bins; |
|||
@bins[one_of_n($n)]++ for ^$trials; |
|||
@bins; |
|||
} |
|||
say one_of_n_test();</lang> |
|||
Output: |
|||
<pre>100288 100047 99660 99773 100256 99633 100161 100483 99789 99910</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
<lang python>from random import random as rnd |
<lang python>from random import random as rnd |