问题
I'd like to assign a random value to a variable in a velocity template. However,
#set ($random = $math.getRandom())
returns: java.util.Random@5706937e. How to obtain this value or assign a random value to a variable in a different way from within a velocity template?
回答1:
#set ($random = $math.getRandom())
use velocity 1.7 from this link enter link description here
and in vm file use
#set($mathTool = $serviceLocator.findService("org.apache.velocity.tools.generic.MathTool"))//range: 1 - 20
#set($my_rnd=$mathTool.random(1,20))or
//range: 0 - 1
#set($my_rnd=$mathTool.getRandom())来源:https://stackoverflow.com/questions/24758937/velocity-random-number-generation-in-a-template