in reply to Re: Re: Re: Select three random numbers between 1..10
in thread Select three random numbers between 1..10

perl -le '@l=sort{rand$_<=>rand$_}(1..10);print for@l[0..2]'
In older Perls, it's not garanteed to give any meaningful results - in fact, perl might as well segfault. In modern Perls, about all that's garanteed is that perl won't segfault, and perl won't internally get corrupted. I'm not at all convinced that the above code produces fair results. Here's a test with a smaller array that strongly suggests your algorithm isn't fair:
#!/usr/bin/perl use strict; use warnings; no warnings qw /syntax/; my @array = (1 .. 4); my %count; my $mult = 10_000; for (1 .. 24 * $mult) { my @t = sort {rand 1 <=> rand 1} @array; $count {"@t"} ++; } foreach my $key (sort keys %count) { printf "%5d: %s\n" => $count {$key}, $key } __END__ 15063: 1 2 3 4 15069: 1 2 4 3 7419: 1 3 2 4 7615: 1 3 4 2 7602: 1 4 2 3 7606: 1 4 3 2 14978: 2 1 3 4 14923: 2 1 4 3 7497: 2 3 1 4 7460: 2 3 4 1 7558: 2 4 1 3 7401: 2 4 3 1 7485: 3 1 2 4 7484: 3 1 4 2 7585: 3 2 1 4 7577: 3 2 4 1 14876: 3 4 1 2 14968: 3 4 2 1 7467: 4 1 2 3 7544: 4 1 3 2 7461: 4 2 1 3 7477: 4 2 3 1 14853: 4 3 1 2 15032: 4 3 2 1

Abigail

Replies are listed 'Best First'.
Re: Re: Select three random numbers between 1..10
by waswas-fng (Curate) on Mar 16, 2004 at 23:49 UTC
    When golfing... (I did not really care about old perl compatibility)

    A quick trip to the sort perldoc exposes me to:
    The comparison function is required to behave. If it returns inconsistent results (sometimes saying $x[1] is less than $x[2] and sometimes saying the opposite, for example) the results are not well-defined.


    So i guess my golf does not produce completely random picks thanks for the heads up... ohh well =)


    -Waswas