生成随机矩阵

时间:2014-05-30 16:44:26

标签: perl random matrix

如何在Perl中生成一个矩阵(对引用数组的引用)X by Y?

2 个答案:

答案 0 :(得分:2)

my $x = 5;
my $y = 3;
my $val = 23;

my @matrix = (
  map [ ($val) x $x ], 1 .. $y
);

use Data::Dump;
dd @matrix;

输出:

(
  [23, 23, 23, 23, 23],
  [23, 23, 23, 23, 23],
  [23, 23, 23, 23, 23],
)

答案 1 :(得分:0)

结帐perldoc perllol

my @random = map [ map rand, 1..5 ], 1..10; # Generates a 5x10 matrix of random values