Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Perl中从数组中获得加权随机选择?_Perl_Random - Fatal编程技术网

如何在Perl中从数组中获得加权随机选择?

如何在Perl中从数组中获得加权随机选择?,perl,random,Perl,Random,我需要从数组中随机抽取一些元素。我将索引$array[intrand100]随机化。我希望一些元素更频繁地出现。我该怎么做 我想到了一个愚蠢的解决方案,将这些元素在数组中复制几次,但我相信你们可以做得更好。您想生成一个新的。相关问题包括替换和不替换 如果知道随机数出现的频率,可以使用rand函数。假设您希望数字0在33%的时间内出现,1在其他66%的时间内出现。然后检查rand是否小于0.33,并返回一些索引。否则,返回另一个索引。这只是一种方法。另一种方法是采用类似于以下内容的结构:请原谅我的

我需要从数组中随机抽取一些元素。我将索引$array[intrand100]随机化。我希望一些元素更频繁地出现。我该怎么做


我想到了一个愚蠢的解决方案,将这些元素在数组中复制几次,但我相信你们可以做得更好。

您想生成一个新的。相关问题包括替换和不替换

如果知道随机数出现的频率,可以使用rand函数。假设您希望数字0在33%的时间内出现,1在其他66%的时间内出现。然后检查rand是否小于0.33,并返回一些索引。否则,返回另一个索引。这只是一种方法。

另一种方法是采用类似于以下内容的结构:请原谅我的语言,我实际上对Perl不太了解

[
  (1, 10),
  (2, 50),
  (3, 80),
  (4, 100)
]

然后,当您从intrand100中获取值时,可以依次将其与第二个元素中的每个元素进行比较,并返回第一个元素。

提供了从任意分布生成随机数的理论

似乎一种相当自然的方法是设置二进制搜索。假设有一群人参加了抽奖活动,人们可以根据自己的意愿多次提交自己的名字。我们的名称如下,提交的材料数量如下:

朱丽叶:2 珍妮:11 杰西卡:7 1月1日 简:1 琴:5 现在,如果我们想从包中随机选择一个名称,我们只需为每个名称分配一个从0开始的范围:

朱丽叶:0,1 珍妮:2,12 杰西卡:13,19 1月20日,20日 简:21,21 琼:22,26 好的,我们有一个相邻范围的数组,每个范围都在0到26之间。我们使用改进的二进制搜索来查找目标项伪代码:

let raffle := { Juliet: 0, 1;
                Jenny: 2, 12;
                Jessica: 13, 19;
                Jan: 20, 20;
                Jane: 21, 21;
                Jean: 22, 26 }

let search minIndex maxIndex rangeValue =
    if minIndex > maxIndex then
        failwith "Not found"

    let selectedIndex = (minIndex + maxIndex) / 2
    let item = raffle[selectedIndex]

    if item.range.min >= rangeValue && item.range.max <= rangeValue
        return item.name
    elif item.range.min < rangeValue
        return search minIndex (selectedIndex - 1) rangeValue
    else
        return search (selectedIndex + 1) maxIndex rangeValue

最普遍的解决方案是一个充当逆函数的函数:一个从0.0到1.0的均匀分布映射到您想要的任何分布的函数


朱丽叶给出了一个很好的方法来实现其中的一个

此sub'获取元素和权重的数组-A10B30C5,并根据权重随机选择一个元素

苏伯兰{

my $line=$_[0];#get the elements array
my @b = split(/\(\d+\)/,$line);#b now holds each elemnet from the array
my @a = "";
my $i=0;
my $tmp;
    while ($line=~m/(\(\d+\)+)/g) {
        $tmp=$1;#temp gets the weight
        if ($tmp=~m/\d+/g) {
            if ($i>0){
            $a[$i]=$&+$a[$i-1];#if weight is grather then 0 -each cell of
            #a sums up the wheights up to it 
            }
            else {
            $a[$i]=$&;
            }
        }
        $i++;
         }
        if ($i>1){
            my $n=int(rand($a[$i-1])+1);#rand a number in the boundries of
            #the total weight of all the elements
            my $s=scalar(@b);
            #go through a and compare to the randomized num-then take the
            #element from b
            for ( $i=0;$i<scalar(@b);$i++){
                if($n<=$a[$i])
                {
                    $c=$b[$i];
                last;
                    }
            }
        } else {
        $c=$b[0];#if only one element
        }

        return $c;
} 你这样称呼潜艇:

我的$rand=我们的randA10B30C5D17; -$rand=B