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中使用Rand对列表进行随机化_Perl_Cgi - Fatal编程技术网

在perl中使用Rand对列表进行随机化

在perl中使用Rand对列表进行随机化,perl,cgi,Perl,Cgi,您好,我目前正在使用List::Util shuffle来随机化带有CGI的数组,但是我想修改代码以使用rand 这是我的密码 print "Content-type: text/html\n\n"; use List::Util qw(shuffle); @haikuone = ('behind', 'the', 'red', 'barn'); @haikutwo = ('prairie', 'grasses', 'reclaiming'); @haikuthree = ('the', 'b

您好,我目前正在使用List::Util shuffle来随机化带有CGI的数组,但是我想修改代码以使用rand

这是我的密码

print "Content-type: text/html\n\n";
use List::Util qw(shuffle);
@haikuone = ('behind', 'the', 'red', 'barn');
@haikutwo = ('prairie', 'grasses', 'reclaiming'); 
@haikuthree = ('the', 'basketball', 'court');

@randomize1 = shuffle(@haikuone);
@randomize2 = shuffle(@haikutwo);
@randomize3 = shuffle(@haikuthree);

print "<html>\n";
print "<head><title>Haiku_Random</title></head>\n";
print "<body>\n";
print "<pre>\n";
print "RANDOM HAIKU (DISCLAIMER: NONSENSE MAY OCCUR)\n";

print "@randomize1\n";

print "@randomize2\n";

print "@randomize3\n";
现在当我这么做的时候

sub fisher_yates_shuffle {
    my $deck = shift;  # $deck is a reference to an array
    return unless @$deck; # must not be empty!

    my $i = @$deck;
    while (--$i) {
        my $j = int rand ($i+1);
        @$deck[$i,$j] = @$deck[$j,$i];
    }
}

my @randomize1 = @haikuone;
fisher_yates_shuffle(\@randomize1);
print "@randomize1\n";
它将打印Haikoune,但不会随机化

始终严格使用;使用警告;!您有以下代码,但没有任何名为@Haikoune、@haikutwo、@haikutwo、@line1、@line2或@line3的数组

@random1 = $line1[rand @haikuone];
@random2 = $line2[rand @haikutwo];
@random3 = $line3[rand @haikuthree];
使用三个数组,每个数组有一个元素,这也很奇怪

您好,我目前正在使用List::Util shuffle随机化数组 使用CGI

这是有道理的。在Perl中,无论您是否正在编写CGI程序,列表::Util::shuffle都是洗牌列表的最佳方式

但是,我想修改代码,改为使用rand

这没有道理。兰德不会乱洗列表。它只是生成一个随机数

使用rand从数组中提取单个随机元素是个好主意

sub fisher_yates_shuffle {
    my $deck = shift;  # $deck is a reference to an array
    return unless @$deck; # must not be empty!
    my $i = @$deck;
    while (--$i) {
        my $j = int rand ($i+1);
        @$deck[$i,$j] = @$deck[$j,$i];
    }
}
但那不是你想要做的

如果您真的想使用rand,那么您需要将它的使用合并到一个函数中。在这个问题的答案中,Perl FAQ中给出了一个很好的函数——因此,在询问以下问题之前,您可能应该先看一下FAQ:

但请注意,这是用Perl实现的。List::Util中的shuffle函数是用C编写的,因此速度会更快


所以,总而言之,不使用List::Util::shuffle确实没有什么好的理由。

List::Util有什么问题?最坏的情况是,复制并粘贴List::Util的功能。这些年来,我学到了一件事:有很多方法可以让列表乱序出错。您应该始终使用被广泛认为是好的现有解决方案。请参阅上面的更新后最坏情况场景,复制并粘贴List::Util所做的事情—List::Util::shuffle在XS中实现。所以复制代码可能并不是那么简单:好吧,现在当我这么做的时候,它不会随机化。查看上面更改的代码您没有这样做。我要重复一次:永远严格使用;使用警告;!我更新了这篇文章,上面还有三个错误,它会告诉你的!我有数组,看上面我从没说过你没有数组。从严格使用开始;使用警告;。它将显示您正在使用的所有变量,而无需声明,这与您的问题直接相关。
@random1 = $line1[rand @haikuone];
@random2 = $line2[rand @haikutwo];
@random3 = $line3[rand @haikuthree];
my $random_element = @array[rand @array];
sub fisher_yates_shuffle {
    my $deck = shift;  # $deck is a reference to an array
    return unless @$deck; # must not be empty!
    my $i = @$deck;
    while (--$i) {
        my $j = int rand ($i+1);
        @$deck[$i,$j] = @$deck[$j,$i];
    }
}