Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env lua
- --Lua random # generator module
- --Seeded using any string
- --Requires Lua 5.2 (bit32 library)
- local _M={}
- _M.new = function(seed)
- seed = tostring(seed) .. "#*]"
- assert(#seed >= 4, "Seed too short")
- local seeds = {0xdeadbeef,0x67898765,0xfacecafe,#seed}
- for i = 1, #seed do
- local chr = string.byte(seed:sub(i))
- for s = 1, 4 do
- if s > 1 then
- for s2 = 1, s-1 do
- seeds[s]=seeds[s] + seeds[s2]
- end
- end
- seeds[s] = bit32.lrotate((seeds[s]+chr), (seeds[s] + chr) % 8 + 5)
- end
- end
- local w,x,y,z = seeds[4],seeds[3],seeds[2],seeds[1]
- if(w+x+y+z == 0) then
- w,x,y,z = 0xdeadbeef,0xfacecafe,0x12345678,999
- end
- local fun = function(arg)
- local t = bit32.bxor(x, bit32.lshift(x,11))
- x,y,z = y,z,w
- w = bit32.bxor(w, bit32.rshift(w,19), t, bit32.rshift(t,8))
- if arg == "bin" then
- return w,x,y,z
- end
- local float = bit32.rshift(w,1) / 0x80000000 --31bit float
- if not arg then
- return float
- end
- return math.floor(float * arg) + 1
- end
- --fun()
- return fun
- end
- return _M
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement