Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@airblade
Last active May 13, 2016 11:02
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 airblade/7e5ffb1762548949e189c021c4c13cc7 to your computer and use it in GitHub Desktop.
Save airblade/7e5ffb1762548949e189c021c4c13cc7 to your computer and use it in GitHub Desktop.
Shuffle a list in VimL
" Shuffles list in place.
function! Shuffle(list)
" Fisher-Yates-Durstenfeld-Knuth
let n = len(a:list)
for i in range(0, n-2)
let j = Random(0, n-i-1)
let e = a:list[i]
let a:list[i] = a:list[i+j]
let a:list[i+j] = e
endfor
return a:list
endfunction
" Returns a pseudorandom integer i such that 0 <= i <= max
function! Random(min, max)
if has('unix')
let i = system('echo $RANDOM') " 0 <= i <= 32767
else
let i = system('echo %RANDOM%') " 0 <= i <= 32767
endif
return i * (a:max - a:min + 1) / 32768 + a:min
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment