Last active
May 13, 2016 11:02
-
-
Save airblade/7e5ffb1762548949e189c021c4c13cc7 to your computer and use it in GitHub Desktop.
Shuffle a list in VimL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 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