http://www.perlmonks.org?node_id=1027141


in reply to Re: Non-Repetitive Random Numbers
in thread Non-Repetitive Random Numbers

That doesn't guarantee unique values. With the built-in rand/srand, it produces repeats within 30 picks for seeds:

seed : no.of picks before repeat 89 : 12 178 : 17 485 : 9 504 : 14 555 : 22 662 : 16 688 : 23 761 : 19 766 : 18 920 : 26 936 : 25 943 : 19 955 : 17 982 : 14 1004 : 27 1032 : 19 1192 : 23 1200 : 9 1385 : 24 1427 : 17 1439 : 29 1459 : 22 1582 : 14 1593 : 22 1602 : 29 1668 : 12 1737 : 22 1924 : 29 1933 : 24 1969 : 18 2186 : 27 2261 : 12 2297 : 12 2380 : 18 2410 : 29 2660 : 25 2690 : 28 2733 : 24 2817 : 13 2863 : 4 2875 : 19 2964 : 28 3023 : 27 3052 : 9 3198 : 24 3208 : 23 3240 : 28 3292 : 16 3415 : 2 3529 : 26 3669 : 22 3692 : 29 3741 : 21 3754 : 13 3791 : 23 3805 : 28 3842 : 24 3854 : 8 3956 : 28 4077 : 27 4202 : 20 -- More --

Testcase:

#! perl -slw use strict; for my $i ( 1 .. 100000 ) { my %hash; srand $i; ++$hash{ int rand 100000 } == 2 and print "$i : ", scalar keys %ha +sh while keys %hash < 30; }

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^3: Non-Repetitive Random Numbers
by LanX (Saint) on Apr 05, 2013 at 14:54 UTC
    I don't understand your point (and I'm too much in a hurry ATM to investigate your code)

    Keys of a hash are always unique, in my code collisions of random numbers are simply ignored.

    update

    > That doesn't guarantee unique values.

    maybe this is clearer:

    DB<107> $seen{int rand 10}=1 for 1 .. 1000000 => "" DB<108> keys %seen => (6, 3, 7, 9, 2, 8, 1, 4, 0, 5)

    update
    one could argue that the order of hash -keys isn't random but that wasn't part of the question.

    Otherwise thats a good occasion to use shuffle or pushing into an array (but this implies reseeding rand every time)

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Keys of a hash are always unique,

      You're right of course.

      I was thinking about an iterator which would need to check whether a value had been seen before but ...


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.