-- mt19937ar-lua @9e110f2d1c8a981c1d4ffccb93e25aef9e8b5f17 | /mt19937ar.lua | https://github.com/davebollinger/mt19937ar-lua | License: MIT License | Minified using https://www.npmjs.com/package/luamin/v/1.0.4
-- Copyright (c) 2016 David Bollinger (davebollinger at gmail dot com)
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
local a=math.floor;local function b(c,d)local e=a(c%65536)local f=a(c/65536)%65536;local g=a(d%65536)local h=a(d/65536)%65536;local i=e*g;local j=e*h;local k=f*g;local l=j+k;return a((l*65536+i)%4294967296)end;local function m(c,d)local n,o=0,1;for p=0,31 do local q=c%2;local r=d%2;if q>0 and r>0 then n=n+o end;if q>0 then c=c-1 end;if r>0 then d=d-1 end;c=c/2;d=d/2;o=o*2 end;return n end;local function s(c,d)local n,o=0,1;for p=0,31 do local q=c%2;local r=d%2;if q>0 or r>0 then n=n+o end;if q>0 then c=c-1 end;if r>0 then d=d-1 end;c=c/2;d=d/2;o=o*2 end;return n end;local function t(c,d)local n,o=0,1;for p=0,31 do local q=c%2;local r=d%2;if q~=r then n=n+o end;if q>0 then c=c-1 end;if r>0 then d=d-1 end;c=c/2;d=d/2;o=o*2 end;return n end;local u=function(v)return a(v/2)end;local w=function(v)return a(v/1073741824)end;local x=function(v)return a(v/2048)end;local y=function(v)return v*128 end;local z=function(v)return v*32768 end;local A=function(v)return a(v/262144)end;local B=function(v)return v%2 end;local C=624;local D=397;local E=0x9908B0DF;local F=0x80000000;local G=0x7FFFFFFF;local H={}function H.new()local I={}local J={}local K=C+1;function I:init_genrand(L)J[0]=m(L,0xFFFFFFFF)for p=1,C-1 do J[p]=b(1812433253,t(J[p-1],w(J[p-1])))+p;J[p]=m(J[p],0xFFFFFFFF)end;K=C end;function I:init_by_array(M,N)self:init_genrand(19650218)if not N then N=#M end;local p,O,P=1,0,C>N and C or N;while P>0 do J[p]=t(J[p],b(t(J[p-1],w(J[p-1])),1664525))+M[O+1]+O;J[p]=m(J[p],0xFFFFFFFF)p,O=p+1,O+1;if p>=C then J[0]=J[C-1]p=1 end;if O>=N then O=0 end;P=P-1 end;for P=C-1,1,-1 do J[p]=t(J[p],b(t(J[p-1],w(J[p-1])),1566083941))-p;J[p]=m(J[p],0xFFFFFFFF)p=p+1;if p>=C then J[0]=J[C-1]p=1 end end;J[0]=0x80000000 end;function I:genrand_int32()local v;if K>=C then if K==C+1 then self:init_genrand(5489)end;for Q=0,C-D-1 do v=s(m(J[Q],F),m(J[Q+1],G))J[Q]=t(J[Q+D],t(u(v),B(v)*E))Q=Q+1 end;for Q=C-D,C-2 do v=s(m(J[Q],F),m(J[Q+1],G))J[Q]=t(J[Q+D-C],t(u(v),B(v)*E))Q=Q+1 end;v=s(m(J[C-1],F),m(J[0],G))J[C-1]=t(J[D-1],t(u(v),B(v)*E))K=0 end;v=J[K]K=K+1;v=t(v,x(v))v=t(v,m(y(v),0x9D2C5680))v=t(v,m(z(v),0xEFC60000))v=t(v,A(v))return v end;function I:genrand_int31()return a(self:genrand_int32()/2)end;function I:genrand_real1()return self:genrand_int32()*1.0/4294967295.0 end;function I:genrand_real2()return self:genrand_int32()*1.0/4294967296.0 end;function I:genrand_real3()return(self:genrand_int32()+0.5)*1.0/4294967296.0 end;function I:genrand_res53()local c=a(self:genrand_int32()/32)local d=a(self:genrand_int32()/64)return(c*67108864.0+d)*1.0/9007199254740992.0 end;I.randomseed=I.init_genrand;function I:random(R,S)if not R then return self:genrand_real2()else if not S then return self:genrand_int32()%R+1 else return R+self:genrand_int32()%(S-R+1)end end end;function I:getState()local n={}n.mti=K;n.mt={}for p=0,C-1 do n.mt[p]=J[p]end;return n end;function I:setState(L)if L==nil then return end;if type(L.mti)=="number"then K=a(L.mti)end;if type(L.mt)=="table"then for p=0,C-1 do if type(L.mt[p])=="number"then J[p]=a(L.mt[p])end end end end;function I:createValidationOutput()local T="mt19937ar.lua.out"local U=io.open(T,"wb")self:init_by_array({291,564,837,1110})U:write("1000 outputs of genrand_int32()\n")for p=0,999 do U:write(string.format("%10.0f ",self:genrand_int32()))if p%5==4 then U:write("\n")end end;U:write("\n1000 outputs of genrand_real2()\n")for p=0,999 do U:write(string.format("%10.8f ",self:genrand_real2()))if p%5==4 then U:write("\n")end end;U:close()end;return I end;return H
