Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Is there a better way to generate unique set of random numbers ?

by moritz (Cardinal)
on Jul 28, 2011 at 09:44 UTC ( [id://917231]=note: print w/replies, xml ) Need Help??


in reply to Is there a better way to generate unique set of random numbers ?

I'd do it like this:
use strict; use warnings; my @random_set; my %seen; for (1..10) { my $candidate = int rand(1185); redo if $seen{$candidate}++; push @random_set, $candidate; } print join(', ', @random_set), "\n";

If you need to generate many more than 10 distinct random numbers, this solution will be faster because the hash lookup works in constant time, whereas your solution has to iterate over the whole array for each number. For 10 there won't be a big difference.

Also note that for testing it is better to reduce the maximum number so that collision become more likely - I tested it with 12 instead of 1185.

Update: If the maximal number is the same or not much larger than the number of random values you want, this would probably be faster:

use List::Util qw/shuffle/; my @random_set = (shuffle 0..11)[0..9];

That is, first generating a random permutation, and then selecting some of the elements.

But as always it's best to Benchmark the different solutions with real-world data.

Replies are listed 'Best First'.
Re^2: Is there a better way to generate unique set of random numbers ?
by JavaFan (Canon) on Jul 28, 2011 at 10:02 UTC
    If you need to generate many more than 10 distinct random numbers, this solution will be faster because the hash lookup works in constant time, whereas your solution has to iterate over the whole array for each number. For 10 there won't be a big difference.
    But do note that the only reason your algorithm guarantees to terminate is because rand uses a pseudo random number generator. Were rand to be truly random, neither algorithm could guarantee to be finished before Perl 6 dominates the world.

    Here's an algorithm that does terminate:

    use List::Util 'shuffle'; my @set = (shuffle(0..1184))[0..9];
      Were rand to be truly random, neither algorithm could guarantee to be finished before Perl 6 dominates the world.

      Though if you do the math and calculate the probability that either algorithm does not terminate with the given parameters in, let's say, 2 seconds on a modern machine, you'll probably find that the chance of computation errors in the CPU caused by cosmic rays is much higher.

      Doing the strict theory only makes sense if the actual machine corresponds to the machine model that the theory assumes.

        use List::Util; sub unique_random_list { my ($from, $to, $count) = @_; $count = List::Util::min ($count, $to - $from); my @result; my @queue = [ $from, $to - $from, $count ]; while (my $job = shift @queue) { my ($from, $length, $count) = @$job; if ($count == $length) { push @result, $from .. $from + $length; } elsif ($count == 1) { push @result, $from + int rand $length; } else { my $split_length = int ($length / 2); my $split_count = List::Util::min (int rand $count, $spli +t_length); my $pad_length = $length - $split_length; my $pad_count = $count - $split_count; $split_count += $pad_count - $pad_length if $pad_count > $pad_length; unshift @queue, grep $_->[2], [ $from, $split_length, $split_count ], [ $from + $split_length, $length - $split_length, $count + - $split_count ]; } } @result; } my @list = unique_random_list (10, 100, 30);
        Doing the strict theory only makes sense if the actual machine corresponds to the machine model that the theory assumes.
        Sure, but what's the fun of that? That makes all algorithms either O(1) (aka, "it terminates") or they loop and never terminate.
Re^2: Is there a better way to generate unique set of random numbers ?
by chakreey (Acolyte) on Jul 28, 2011 at 09:58 UTC

    Thank you

    I am still a beginner in perl, can you help me understand $seen{$candidate}++ in line:

    redo if $seen{$candidate}++;
      %seen is a hash. If the key $candidate is not present in the hash, it is added to the hash (that's called "autovivification"), initialized to 1, and undef is returned - the redo is not executed.

      On the other hand if the key is already present in the hash, the corresponding value (for example 1) is returned, and incremented by one (for example set to 2). In this case the interesting part is not the incrementing, but that it returns a true value, so the redo is executed.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://917231]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-19 12:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found