Skip to content

Instantly share code, notes, and snippets.

@aeg
Created February 18, 2013 16:12
Show Gist options
  • Save aeg/4978491 to your computer and use it in GitHub Desktop.
Save aeg/4978491 to your computer and use it in GitHub Desktop.
リストの要素をランダムに並び替える
// Case #1
// リストの要素をランダムに並び替える
def list1 = ['a', 'b', 'c', 'd']
def list2 = []
while(list1.size() > 0) {
index = new Random().nextInt(list1.size())
list2.add(list1[index])
list1.remove(index)
}
println list2
// 結果
// [a, b, d, c]
// Case #2
// リストの要素をランダムに並び替える
list1 = ['a', 'b', 'c', 'd']
Collections.shuffle(list1)
println list1
// 結果
// [d, c, b, a]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment