-2

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.

Plusce
  • 117
  • 2
  • 14
  • 2
    Use 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
  • 3
    See `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 Answers3

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
  • I haven't considered use shuffle like this. +1 – Toto Jun 02 '15 at 13:34
  • 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
  • That doesn't compile, and that returns n+1 results. – ikegami Jun 02 '15 at 17:37
2

You can simply do:

my @r = map int(rand(60)), 0..9;
say Dumper\@r;
ikegami
  • 367,544
  • 15
  • 269
  • 518
Toto
  • 89,455
  • 62
  • 89
  • 125
-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