Re: biased random number picking
by toolic (Bishop) on Jul 12, 2012 at 02:49 UTC
|
List::Util::shuffle will randomly shuffle a list of numbers, like 1 to 100. Use a slice to grab two of the numbers. These are guaranteed not to be the same.
use warnings;
use strict;
use List::Util qw(shuffle);
for (1 .. 5) {
my @nums = (shuffle(1 .. 100))[0 .. 1];
print "@nums\n";
}
| [reply] [d/l] [select] |
|
|
There is no need to repeat the shuffle each time through the loop. Use the next two numbers.
| [reply] |
|
|
That is not the way I interpreted the OP's specification. If the OP had explicitly stated that no pair should contain any repeat number as another pair, then there is no need to have the shuffle in the loop.
| [reply] |
|
|
In fact the solution is incorrect unless you do this ... and correct if you do.
| [reply] |
Re: biased random number picking
by frozenwithjoy (Priest) on Jul 12, 2012 at 03:40 UTC
|
So, essentially you want to sample w/o replacement. If you don't want to make a complete copy of your array (because it is very large or whatever), you can use the random_permuted_index() method from Math::Random. Another benefit to this module is that (as I understand it), the randomization is 'more random' than for rand; however, it may not be an issue in your case. Also, if you want to keep grabbing two at a time over and over, try splice regardless of how you do your sampling. (Splice is like shift/pop on 'roids.) Here is an example:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
use Math::Random;
my @array = qw( cat dog shark frog chimp seahorse );
my @permuted_index = random_permuted_index( scalar(@array) );
print "Here is your entire set:\n";
print Dumper @array;
print "Here is a random pick of two:\n";
print Dumper @array[ splice( @permuted_index, 0, 2 ) ];
print "And another two (unique from 1st two):\n";
print Dumper @array[ splice( @permuted_index, 0, 2 ) ];
print "And the final two:\n";
print Dumper @array[ splice( @permuted_index, 0, 2 ) ];
Sample output:
| [reply] [d/l] [select] |
Re: biased random number picking
by Athanasius (Archbishop) on Jul 12, 2012 at 03:24 UTC
|
use v5.14;
use strict;
use warnings;
my @options = (1 .. 100);
for (1 .. 100)
{
my $choice = int(rand($#options + 1));
say 'Choice #', $_, ' is ', $options[$choice];
@options = @options[ 0 .. ($choice - 1), ($choice + 1) .. $#option
+s ];
}
HTH,
Athanasius <°(((>< contra mundum
| [reply] [d/l] |
|
|
the slice is expensive and not really required:
my $choice = int(rand($#options + 1));
say 'Choice #', $_, ' is ', $options[$choice];
$options[$choice] = $options[-1];
pop @options;
| [reply] [d/l] |
Re: biased random number picking
by brx (Pilgrim) on Jul 12, 2012 at 07:32 UTC
|
| [reply] [d/l] |
Re: biased random number picking
by RichardK (Parson) on Jul 12, 2012 at 10:31 UTC
|
my @values = (100..200);
my $i = int( rand(@values) );
my $j = $i;
$j = int( rand(@values) ) while $i == $j;
| [reply] [d/l] |
|
|
| [reply] [d/l] |
|
|
| [reply] |
|
|