Basically I want to know if there's a way to pull in a randomly generated number. I know I can do it in JS, just wondering if there's a way of doing it in Velocity.
Just a thought, but can you get the time, in milliseconds in a macro? If you could do that, you can take the last digit. That should be random between 1 and 0, shouldn't it?
It's funny. I forgot I posted this and then came back to the boards to search for answer and lo and behold, this is the best answer.
The random number method that was mentioned below didn't translate to anything but this method works.
The only thing to note is that you can only use it once per User Macro call (or so it seems). So if, like me, you're using this for IDs then you'll have to consider if this right for you. However, it works for me because I am only calling it once per macro (and then calling that macro several times in a page).
Thanks Matthew!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
heh, since then I found a way to generate random numbers in a VM file with the MathTool class.
Add the org.apache.velocity.tools.generic.MathTool class to the Velocity context -- instructions are here (you'll need to modify the XML file to get the Math class rather than the Sorter that the instructions use): https://answers.atlassian.com/questions/62575/use-of-velocity-generic-tools-in-a-page-decorator
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I've used this approach in several user macros to help make unique names for elements:
#set($someUniqueId=$action.random)
It generates a number with between 8 and 10 digits which appears to be random. The getRandom() method for the Action object was deprecated some time ago according to the documentation, but it does not mention the "random()" method.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, the mathtool class has a Rand() function for velocity templates
http://velocity.apache.org/tools/devel/generic/MathTool.html
Cheers,
Andrew
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks, but I can't seem to call it in a user macro. What's the correct notation? I tried $math.random(1,10) for a random number between 1 and 10.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
ah, in a macro I'm afraid that can't be done by default. Bellow is a link to the context which are provied in the user macro creation process.
https://developer.atlassian.com/display/CONFDEV/Confluence%20Objects%20Accessible%20From%20Velocity
And unfortunatley, the Math class is not among them. The only way this could be done is if we overrode the class that creates context by defualt in Confluence and added the math class...which I am not sure how to do myself
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.