Skip to content

Instantly share code, notes, and snippets.

@gabrielfurini
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielfurini/6f45bafc1c863d4e6fd3 to your computer and use it in GitHub Desktop.
Save gabrielfurini/6f45bafc1c863d4e6fd3 to your computer and use it in GitHub Desktop.
Randomizing an array in Java Velocity using Fisher-Yates shuffle method
#set($array = [0..10])
#foreach($i in $array)
#if($i.class != "class java.lang.Integer")
#set($i = $math.sub($velocityCount, 1))
#end
#if($i < $array.size())
#set($j = $math.random(0, $math.add($i, 1)))
#set($temp = $array.get($i))
#set($array[$i] = $array.get($j))
#set($array[$j] = $temp)
#end
#end
Randomized array: ${array}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment