Created
November 22, 2012 23:22
-
-
Save loreb/4133299 to your computer and use it in GitHub Desktop.
vim RNG (/dev/urandom)
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
" I like picking a random colorscheme, but I hate the way it's done in most vim scripts | |
" -- localtime(), system("echo $RANDOM"), etc etc. | |
" And I wanted to try this gist thing... | |
function! DevUrandom() | |
let rv = 0 | |
" readline reads 4096 bytes at a time(think fread()) | |
" and returns a *list* of lines. | |
" Don't only read one line because: | |
" - we read more anyway, and | |
" - a short line means 0 has 1/256 probability rather than 1/4G! | |
for line in readfile('/dev/urandom', 'b', 4) | |
let i = 0 | |
" use any hash function you wish | |
while(i < len(line)) | |
let rv = or(rv * 256, (rv / 256) % 256) " rotate 8 bits | |
let rv = xor(rv, char2nr(line[i])) " xor lower bits | |
let i = i + 1 | |
endwhile | |
endfor | |
" int32 overflow! | |
if(rv < 0) | |
let rv = 0 - (rv + 1) " think INT32_MIN | |
endif | |
return rv | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment