[Solved] Random flling the array – perl


I’m assuming you meant “ten non-negative integers less than 60”.

With possibility of repeats:

my @rands = map { int(rand(60)) } 1..10;

For example,

$ perl -E'say join ",", map { int(rand(60)) } 1..10;'
0,28,6,49,26,19,56,32,56,16       <-- 56 is repeated

$ perl -E'say join ",", map { int(rand(60)) } 1..10;'
15,57,50,16,51,58,46,7,17,53

$ perl -E'say join ",", map { int(rand(60)) } 1..10;'
13,57,26,47,30,14,47,55,39,39     <-- 47 and 39 are repeated

Without possibility of repeats:

use List::Util qw( shuffle );

my @rands = (shuffle 0..59)[0..9];

For example,

$ perl -MList::Util=shuffle -E'say join ",", (shuffle 0..59)[0..9];'
13,50,8,21,11,24,28,51,55,38

$ perl -MList::Util=shuffle -E'say join ",", (shuffle 0..59)[0..9];'
1,0,58,46,47,49,52,33,5,13

$ perl -MList::Util=shuffle -E'say join ",", (shuffle 0..59)[0..9];'
19,43,45,49,23,53,2,38,59,35

7

solved Random flling the array – perl