Vim script で乱数の生成
下記のサイトに Vim script で乱数を生成する方法が載っているのですが、それを元にして C++っぽい生成器を書いてみました。
[ソース]
function! s:srand(seed) let self = {} let self.seed = a:seed function self.apply() let self.seed = self.seed * 214013 + 2531011 return (self.seed < 0 ? self.seed - 0x80000000 : self.seed) / 0x10000 % 0x8000 endfunction return self endfunction function! s:uniform_int_distribution(min, max) let self = {} let self.min = a:min let self.max = a:max function! self.apply(engine) let num = a:engine.apply() return num % ((self.max+1)-self.min) + self.min endfunction return self endfunction function! s:discrete_distribution(list) let self = {} let self.list = a:list function! self.apply(engine) let num = a:engine.apply() return self.list[ num % len(self.list) ] endfunction return self endfunction function! s:variate_generator(engine, dist) let self = {} let self.engine = a:engine let self.dist = a:dist function! self.get() return self.dist.apply(self.engine) endfunction return self endfunction function! s:main() let engine = s:srand(localtime()) let distribution = s:uniform_int_distribution(1, 10) echo map(range(30), "distribution.apply(engine)") let list = ["homu", "mami", "mado", "saya", "an"] let list_dist = s:discrete_distribution(list) for n in range(10) echo list_dist.apply(engine) endfor endfunction call s:main()