I need to know how can I fill the array in perl randomly. For example: I want declare an array containing 10 elements smaller than 60. Can someone explain me how can I do it or send me any guide? I would be so grateful.
Asked
Active
Viewed 232 times
-2
-
2Use a loop and the push command to put a new random number into the array with each loop iteration. – Neil H Watson Jun 02 '15 at 12:55
-
3See `shuffle()` in `perldoc List::Util` http://perldoc.perl.org/List/Util.html#@values-=-shuffle-@values `List::Util` is a standard module that is installed with Perl. For a list of all the standard modules, see `perldoc perlmodlib` http://perldoc.perl.org/perlmodlib.html – shawnhcorey Jun 02 '15 at 13:09
-
@shawnhcorey: `shuffle` returns a list in random order, it doesn't build random values. – Toto Jun 02 '15 at 13:23
-
1@Toto, You'd use `shuffle` if you wanted all ten elements to be different. See my answer. – ikegami Jun 02 '15 at 13:28
-
@Toto: See the first link in my above comment. It has an example of how to populate randomly with the numbers 0 .. 51. – shawnhcorey Jun 02 '15 at 14:24
3 Answers
5
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

ikegami
- 367,544
- 15
- 269
- 518
-
-
So when I want fill an array contain 'n' elements, can I use: "@rand = map {int (rand(60)) } n;"? Or create a loop 'for' and enter counts in this way?: "my $tab[$i] = map {int (rand(60)) };". I used second option before the moment and my compiler showed errors. What's wrong? – Plusce Jun 02 '15 at 15:13
-
The first will only assign one element, and I'd be hard pressed to find something that isn't wrong with the second snippet. – ikegami Jun 02 '15 at 15:17
-
So if nothing is wrong what about the compiler errors? Furthermore it seems my compiler isn't read prefix "my" (is not coloured) – Plusce Jun 02 '15 at 15:38
-
I didn't say nothing was wrong; I said it was completely wrong. It's like someone made up a sentence by picking random words from a dictionary. – ikegami Jun 02 '15 at 15:45
-
Indeed, I misinterpreted. Finally I used this way: "my @tab = { int (rand(60)) 1..$n+1". Work irreproachable. – Plusce Jun 02 '15 at 17:36
-
2
You can simply do:
my @r = map int(rand(60)), 0..9;
say Dumper\@r;
-
-
-
You could save more by using [Data::Printer](http://p3rl.org/Data::Printer) and even get free colos! `p @r` =) – simbabque Jun 02 '15 at 13:24
-1
You could take advantage of perl's random sorting of hash keys. This will fill an array of 10 elements randomly each time you run it:
use warnings;
use strict;
my @nums = (1 .. 60);
my %data;
$data{$_}++ foreach @nums;
my $count = 0;
my @random;
foreach (keys %data){
$count++;
push @random, $_ if $count <= 10;
}

fugu
- 6,417
- 5
- 40
- 75
-
-
1
-
1@Toto, Not in newer versions of Perl, and not always in old versions of Perl, but I agree that's not the way to go. For starters, I think it's not completely random. – ikegami Jun 02 '15 at 13:38
-
@simbabque - I would say it's the least arcane answer here. It's also easier to understand for someone new to perl than `map` etc – fugu Jun 02 '15 at 13:40
-
2I don't agree with that, but I think that that discussion is best had over a beer or two. ;) – simbabque Jun 02 '15 at 13:42
-
2
-
2It's moot talking about readability when you don't even have a working solution. Feel free to replace the `map` with a more complicated `for`+`push`, but don't replace `rand` with hash key orderings. What you posted is **awful**. – ikegami Jun 02 '15 at 15:19
-
1
-
3