How to get a random number in a pattern set?

I want to get a random number using template tools. This should not be particularly random. How can I do it?

+6
template-toolkit
source share
2 answers

From this post in Slashcode :

[ slash@yaz slash]$ perl -MSlash::Test -leDisplay [% digits = [ 0 .. 9 ]; anumber = digits.rand _ digits.rand _ digits.rand; anumber; %] ^D 769 
-one
source share

Hmm, you may have problems if you do not (or cannot import) Slash :: Test. From the "vanilla" TT installation, you can simply use the Math plugin:

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

See this link in the template toolkit manual for more information on the Math plugin and various methods.

Update: Math.rand requires an option. Therefore, to get a random number from 0 to 1, use:

 GET Math.rand(1); 
+6
source share

All Articles