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

Random Number with Velocity

Davin Studer
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.
October 1, 2013

Any thoughts on how to generate a random number with velocity?

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Answer accepted
Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
October 1, 2013

OK, how about this. A simple pseudo-unique value for multiple macros on the same page is the timestamp at which they were rendered. This will always be unique unless:

  • The macros are both rendered within the space of a single millisecond (highly unlikely)
  • The macros are both rendered concurrently (Confluence does not currently do this, but maybe this might change in the long-term future of Confluence?)

Here's my example user macro:

## @noparams
<div>
<strong>Unique Value:</strong>
<em>
$action.dateFormatter.calendar.timeInMillis
</em>
</div>

And what it looks like with two of them of the page:

Davin Studer
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.
October 1, 2013

I had thought about soem knd of timestamp ... that was going to be my next question if the random number thing didn't pan out. ;) If it goes down to the miliseconds that should probably be unique enough. I'll give that a shot. Thanks sir.

1 vote
Timothy
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.
October 1, 2013

Much better to not put "business logic" into the templating engine.

Davin Studer
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.
October 1, 2013

I disagree. This is for a user macro that is going to have proceeding javascript. I need to be able to uniquely identify the html created by the macro just incase the user macro is added multiple times to the page. A sufficently large random number seemed to me to be the easiest way to get a unique number ... since the indvidual macros will not be aware of the state of the others. Unless you can think of some other way to get a unique identifier to put on the html element.

0 votes
Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
October 1, 2013

Unfortunately the Velocity language doesn't let you invoke static methods on classes that cannot be constructed (eg. java.util.Math), so it's not possible to get a velocity template to generate a random number.

You can work around this by having Java code on the server side that populates the Velocity context with the information it needs, but user macros do not allow macro authors to embed Java code in the system, so it's not possible in this case.

This SO question has more info: http://stackoverflow.com/questions/2329191/calling-class-methods-static-from-inside-a-velocity-view-page

Can you use the Javascript side of your macro to generate the random number? Alternatively you may need to add a parameter to the user macro that requires the author to manually supply a random/unique value.

Davin Studer
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.
October 1, 2013

Adding the random number via javascript would not be useful. The whole pont was so that I could put a unique id on an html element and them target that with the javascript contained in the user macro. Otherwise if the uer adds this user macro multiple times to a page then the JS in each would affect all of them. I'm trying to get a unique value that I can put in the element and pass to the JS, so that the scripting is targeted to that specific user macro. Does that make sense. The randome number thing was just a thought on how I might get a unique-ish value ... if the random number was sufficently large. If there is another way to get a unique value that i can put in the html and JS then I would be all for that.

TAGS
AUG Leaders

Atlassian Community Events