Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Answer to: http://stackoverflow.com/a/6557893/98057
- Tested with PHP 5.4.13
- */
- function fisherYatesShuffle(&$items, $seed=null)
- {
- if ($seed !== null) {
- @mt_srand($seed);
- }
- for ($i = count($items) - 1; $i > 0; $i--)
- {
- $j = @mt_rand(0, $i);
- $tmp = $items[$i];
- $items[$i] = $items[$j];
- $items[$j] = $tmp;
- }
- }
- $arr = [0, 1,2,3,4,5,6,7,8,9];
- fisherYatesShuffle($arr, 0);
- print_r($arr);
- /* will print:
- Array
- (
- [0] => 6
- [1] => 0
- [2] => 7
- [3] => 2
- [4] => 9
- [5] => 3
- [6] => 1
- [7] => 8
- [8] => 5
- [9] => 4
- )
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement