Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Pick k numbers at random

by Athanasius (Archbishop)
on Nov 12, 2019 at 13:06 UTC ( [id://11108584]=note: print w/replies, xml ) Need Help??


in reply to Pick k numbers at random

Hello Chuma,

The core module List::Util has a shuffle function which does what you want:

use strict; use warnings; use Const::Fast; use Data::Dump; use List::Util qw( shuffle ); const my $N => 10; const my $K => 5; my @range = (shuffle 1 .. $N)[0 .. $K - 1]; dd \@range;

Sample output:

23:04 >perl 2029_SoPW.pl [10, 9, 5, 6, 7] 23:04 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-20 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found