in reply to Selecting random records from an array

If it has to be precisely 75% of the count, then other solutions in this thread are suitable. But if you just want "about 75%" of the data, this'll work:
my @subset = grep rand(100) <= 75, @input;

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re: &bull;Re: Selecting random records from an array
by Juerd (Abbot) on Jun 03, 2003 at 22:25 UTC

    my @subset = rand(75) <= 100, @input;

    I think it works better with grep and the values swapped :)

    my @subset = grep rand(100) < 75, @input;
    Or just:
    my @subset = grep rand() < .75, @input;

    Perhaps I misinterpreted your code, but in that case I have no idea what it is supposed to do.

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

      He is creating a throttle or valve with the grep function, allowing only a certain stochastic percentage of @input applicants through the valve into the accepted @subset.

      Think of it as a lottery: anybody who flips a coin twice and gets a "heads" in either flip will win. That could be everyone, it could be nobody, but the odds are that about 75% of the players will win.

      --
      [ e d @ h a l l e y . c c ]

        He is creating a throttle or valve with the grep function, allowing only a certain stochastic percentage of @input applicants through the valve into the accepted @subset.

        I know. merlyn's Original post had my @subset = rand(75) <= 100, @input;, which wasn't using grep at all. Apparently he's updated the node.

        I don't include citations for you to ignore them; they're there to read :)

        Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }