Advertisement
FredMSloniker

makerng.lua

Sep 25th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.16 KB | None | 0 0
  1. --[[------------------------------------
  2. RandomLua v0.3.1
  3. Pure Lua Pseudo-Random Numbers Generator
  4. Under the MIT license.
  5. copyright(c) 2017 linux-man
  6. --]]------------------------------------
  7.  
  8. -- Multiply-with-carry
  9. local multiply_with_carry = {}
  10. multiply_with_carry.__index = multiply_with_carry
  11.  
  12. function multiply_with_carry:random(a, b)
  13.     local m = self.m
  14.     local t = self.a * self.x + self.c
  15.     local y = t % m
  16.     self.x = y
  17.     self.c = math.floor(t / m)
  18.     if not a then return y / m
  19.     elseif not b then
  20.         if a == 0 then return y
  21.         else return 1 + (y % a) end
  22.     else
  23.         return a + (y % (b - a + 1))
  24.     end
  25. end
  26.  
  27. function multiply_with_carry:randomseed(s)
  28.     self.c = self.ic
  29.     self.x = (s or os.time()) % 0x80000000
  30. end
  31.  
  32. local function mwc(s, r)
  33.     local temp = {}
  34.     setmetatable(temp, multiply_with_carry)
  35.     temp.a, temp.c, temp.m = 1103515245, 12345, 0x10000000  --from Ansi C
  36.     if r then
  37.         if r == 'nr' then temp.a, temp.c, temp.m = 1664525, 1013904223, 0x10000000 --from Numerical Recipes.
  38.         elseif r == 'mvc' then temp.a, temp.c, temp.m = 214013, 2531011, 0x10000000 end--from MVC
  39.     end
  40.     temp.ic = temp.c
  41.     temp:randomseed(s)
  42.     return temp
  43. end
  44.  
  45. return mwc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement