Velocity - random number generation in a template

女生的网名这么多〃 提交于 2019-12-25 03:28:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!