随机flling阵列 - perl的 [英] Random flling the array - perl
本文介绍了随机flling阵列 - perl的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要知道我怎么能填充数组在Perl随机。例如:我想声明包含60比10小的要素有人可以解释我怎么能做到这一点,或给我任何指导的数组?我将非常感激。
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.
推荐答案
我假设你的意思是十非负整数小于60。
I'm assuming you meant "ten non-negative integers less than 60".
使用重复的可能性:
my @rands = map { int(rand(60)) } 1..10;
例如,
$ 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];
例如,
$ 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
这篇关于随机flling阵列 - perl的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文