Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[------------------------------------
- RandomLua v0.3.1
- Pure Lua Pseudo-Random Numbers Generator
- Under the MIT license.
- copyright(c) 2017 linux-man
- --]]------------------------------------
- -- Multiply-with-carry
- local multiply_with_carry = {}
- multiply_with_carry.__index = multiply_with_carry
- function multiply_with_carry:random(a, b)
- local m = self.m
- local t = self.a * self.x + self.c
- local y = t % m
- self.x = y
- self.c = math.floor(t / m)
- if not a then return y / m
- elseif not b then
- if a == 0 then return y
- else return 1 + (y % a) end
- else
- return a + (y % (b - a + 1))
- end
- end
- function multiply_with_carry:randomseed(s)
- self.c = self.ic
- self.x = (s or os.time()) % 0x80000000
- end
- local function mwc(s, r)
- local temp = {}
- setmetatable(temp, multiply_with_carry)
- temp.a, temp.c, temp.m = 1103515245, 12345, 0x10000000 --from Ansi C
- if r then
- if r == 'nr' then temp.a, temp.c, temp.m = 1664525, 1013904223, 0x10000000 --from Numerical Recipes.
- elseif r == 'mvc' then temp.a, temp.c, temp.m = 214013, 2531011, 0x10000000 end--from MVC
- end
- temp.ic = temp.c
- temp:randomseed(s)
- return temp
- end
- return mwc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement