[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

http://velocity.apache.org/tools/devel/generic/MathTool.html

Cheers,
Andrew

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.

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

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.

http://docs.atlassian.com/atlassian-confluence/latest/com/atlassian/confluence/core/ConfluenceActionSupport.html#getRandom%28%29


Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Oct 24, 2018 in Confluence

Atlassian Research opportunity with Confluence templates

Do you use templates with Confluence? Take part in a remote 1-hr workshop. You'll receive USD $100 for your time!   We're looking for people to participate in a   remote 1-hr workshop...

1,165 views 20 14
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