mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-02-03 02:20:05 +08:00
86 lines
3.1 KiB
VimL
Vendored
86 lines
3.1 KiB
VimL
Vendored
" =============================================================================
|
|
" Filename: autoload/screensaver/random.vim
|
|
" Author: itchyny
|
|
" License: MIT License
|
|
" Last Change: 2015/02/18 10:07:46.
|
|
" =============================================================================
|
|
|
|
let s:save_cpo = &cpo
|
|
set cpo&vim
|
|
|
|
" Random seed.
|
|
if has('reltime')
|
|
let s:rel = reltime() + reltime()
|
|
if len(s:rel) > 3
|
|
let [s:x, s:y, s:z, s:w; s:rest] = s:rel
|
|
else
|
|
let [s:x, s:y, s:z, s:w] = [1, 2, 3, 4]
|
|
endif
|
|
else
|
|
let [s:x, s:y, s:z, s:w] = [1, 2, 3, 4]
|
|
endif
|
|
|
|
" Random number.
|
|
" screensaver#random#number() : an unbounded random integer number.
|
|
" screensaver#random#number(a) : an unbounded random number larger than a.
|
|
" screensaver#random#number(a, b) : a random number from [a, a + b - 1].
|
|
function! screensaver#random#number(...) abort
|
|
let a = a:0 ? a:1 : 0
|
|
let b = a:0 > 1 ? a:2 : 0x1000000
|
|
let t = s:xor(s:x, (s:x * 0x800))
|
|
let [s:x, s:y, s:z] = [s:y, s:z, s:w]
|
|
let s:w = s:xor(s:xor(s:w, (s:w / 0x80000)), s:xor(t, (t / 0x100)))
|
|
return a + s:w % b
|
|
endfunction
|
|
|
|
" xor function from vital.vim
|
|
if exists('*xor')
|
|
|
|
function! s:xor(a, b) abort
|
|
return xor(a:a, a:b)
|
|
endfunction
|
|
|
|
else
|
|
|
|
function! s:xor(a, b) abort
|
|
let a = a:a < 0 ? a:a - 0x80000000 : a:a
|
|
let b = a:b < 0 ? a:b - 0x80000000 : a:b
|
|
let r = 0
|
|
let n = 1
|
|
while a || b
|
|
let r += s:xor[a % 0x10][b % 0x10] * n
|
|
let a = a / 0x10
|
|
let b = b / 0x10
|
|
let n = n * 0x10
|
|
endwhile
|
|
if (a:a < 0) != (a:b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