如何在模板工具箱中获取随机数?

时间:2009-04-06 18:11:16

标签: template-toolkit

我想使用模板工具包获取一个随机数。它不必特别随机。我该怎么做?

2 个答案:

答案 0 :(得分:6)

嗯,如果你没有(或不能导入)Slash :: Test,你可能会遇到问题。 从TT的“vanilla”安装,您只需使用Math插件:

USE Math;
GET Math.rand; # outputs a random number from 0 to 1

有关Math插件和各种方法的详细信息,请参阅this link in the template toolkit manual

更新:Math.rand需要一个参数。因此,要获得0到1之间的随机数,请使用:

GET Math.rand(1);

答案 1 :(得分:-1)

来自this post at Slashcode

[slash@yaz slash]$ perl -MSlash::Test -leDisplay
[%
digits = [ 0 .. 9 ];
anumber = digits.rand _ digits.rand _ digits.rand;
anumber;
%]
^D
769
相关问题