【发布时间】:2010-10-17 21:04:34
【问题描述】:
我想使用模板工具包获得一个随机数。它不必特别随机。我该怎么做?
【问题讨论】:
标签: template-toolkit
我想使用模板工具包获得一个随机数。它不必特别随机。我该怎么做?
【问题讨论】:
标签: template-toolkit
[slash@yaz slash]$ perl -MSlash::Test -leDisplay
[%
digits = [ 0 .. 9 ];
anumber = digits.rand _ digits.rand _ digits.rand;
anumber;
%]
^D
769
【讨论】:
嗯,如果您没有(或无法导入)Slash::Test,您可能会遇到问题。 从 TT 的“vanilla”安装中,您可以简单地使用 Math 插件:
USE Math;
GET Math.rand; # outputs a random number from 0 to 1
有关数学插件和各种方法的更多信息,请参阅this link in the template toolkit manual。
更新:Math.rand 需要一个参数。因此,要获得一个从 0 到 1 的随机数,请使用:
GET Math.rand(1);
【讨论】: