Skip to content

Instantly share code, notes, and snippets.

@ikaruga777
Last active June 17, 2020 15:14
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 ikaruga777/72e69b0692aa399bce07e80c245e4380 to your computer and use it in GitHub Desktop.
Save ikaruga777/72e69b0692aa399bce07e80c245e4380 to your computer and use it in GitHub Desktop.
shuffle実装した
def rand_once(seed): (48271 * seed) % 2147483647;
def rand(seed): rand_once(rand_once(rand_once(seed)));
def swap(s): . as $a
| if s[0] == s[1] then
.
else
if s[0] > s[1] then [s[1], s[0]] else s end
| .[0] as $x | .[1] as $y
| $a | [ .[:$x], .[$y], .[$x+1:$y], .[$x], .[$y+1:] ] | flatten
end
;
def shuffle(seed): . as $a
| $a | length as $length
| [range(0; $length-1)] | reduce .[] as $i ( $a; . as $x | (rand(seed+$i) % $length) as $r | $x | swap([$r,$i] ))
;
import "./shuffle" as S;
["A", "B", "C", "D", "E", "F", "G"] | S::shuffle(now)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment