[CONF] Is there a way to randomly generate a number in Velocity for a User Macro?

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.

3 answers

1 accepted

1 vote
Accepted answer

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!

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

Yes, the mathtool class has a Rand() function for velocity templates



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.

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.


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

I've used this approach in several user macros to help make unique names for elements:


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.


Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Feb 06, 2019 in Confluence

Try out the new editing experience

Hi team, I’m Avinoam, a product manager on Confluence Cloud, and today I’m really excited to let the Community know that all customers can now try out the new editing experience and see some of the ...

979 views 51 8
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you