Last active
August 29, 2015 14:23
-
-
Save gabrielfurini/6f45bafc1c863d4e6fd3 to your computer and use it in GitHub Desktop.
Randomizing an array in Java Velocity using Fisher-Yates shuffle method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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