- shuffle
- Collections.shuffle
1 Go[ | ]
Go Shuffle() 문서를 참고하십시오.
Go
Copy
package main
import (
"fmt"
"math/rand"
)
func main() {
a := []string{"Apple", "Banana", "Orange", "Mango"}
rand.Shuffle(len(a), func(i, j int) { a[i], a[j] = a[j], a[i] })
fmt.Println(a)
}
Loading
2 Java[ | ]
자바 shuffle() 문서를 참고하십시오.
Java
Copy
import java.util.Arrays;
import java.util.List;
import java.util.Collections;
public class MyClass {
public static void main(String args[]) {
List<String> list = Arrays.asList("Apple", "Banana", "Orange", "Mango");
Collections.shuffle(list);
for (String str : list) {
System.out.println(str);
}
}
}
Loading
3 JavaScript[ | ]
JavaScript
Copy
let array = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
let shuffled = array
.map(a => ([Math.random(),a]))
.sort((a,b) => a[0]-b[0])
.map(a => a[1])
console.log( shuffled );
▶ | ["a", "d", "g", "f", "e", "c", "b"] |
JavaScript
Copy
let array = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
array.map((el, i, arr) => {
let r = ~~(Math.random() * arr.length);
arr[i] = arr[r];
arr[r] = el;
return arr;
})[0];
console.log(array);
▶ | ["c", "f", "g", "e", "b", "d", "a"] |
3.1 jQuery[ | ]
html
Copy
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
(function($){
$.shuffle = function(arr) {
for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
return arr;
}
})(jQuery);
var fruits = ["Apple", "Banana", "Orange", "Mango"];
$.shuffle(fruits);
console.log(fruits);
// ["Orange", "Mango", "Apple", "Banana"]
</script>
4 PHP[ | ]
PHP shuffle() 문서를 참고하십시오.
PHP
Copy
$arr = ["Apple", "Banana", "Orange", "Mango"];
shuffle($arr);
print_r($arr);
Loading
5 Python[ | ]
파이썬 shuffle() 문서를 참고하십시오.
Python
Copy
import random
fruits = ["Apple", "Banana", "Orange", "Mango"]
random.shuffle(fruits)
print( fruits )
Loading
6 Ruby[ | ]
Ruby
Copy
fruits = ["Apple", "Banana", "Orange", "Mango"]
shuffled = fruits.sort{ rand }
puts shuffled
Loading