【问题标题】:How do I get a random number in template toolkit?如何在模板工具包中获取随机数?
【发布时间】:2010-10-17 21:04:34
【问题描述】:

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

【问题讨论】:

    标签: template-toolkit


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      嗯,如果您没有(或无法导入)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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-17
        • 2011-12-26
        • 2012-04-12
        • 2018-01-22
        • 2010-09-16
        • 1970-01-01
        相关资源
        最近更新 更多