Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

Steve Goldberg
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 7, 2012

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
Answer accepted
Matthew J. Horn
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 5, 2012

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?

Steve Goldberg
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 29, 2013

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!

Matthew J. Horn
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 29, 2013

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

2 votes
CharlesH
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 17, 2012

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


2 votes
AndrewA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 4, 2012

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

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

Cheers,
Andrew

Steve Goldberg
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 4, 2012

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.

AndrewA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 5, 2012

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events