Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a random number in template toolkit?

I want to get a random number using template toolkit. It doesn't have to be particularly random. How do I do it?

like image 457
Sam Lee Avatar asked Apr 06 '09 18:04

Sam Lee


1 Answers

Hmm, you might have issues if you don't have (or cannot import) Slash::Test. From a "vanilla" installation of TT, 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 the various methods.

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

GET Math.rand(1);
like image 158
Angelos Avatar answered Oct 16 '22 16:10

Angelos