lua-users home
lua-l archive

PRNG for Lua 4.0?

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I found this set of pseudo random number generators here:

https://love2d.org/forums/viewtopic.php?f=5&t=3424

Here's my attempt to translate it from Lua 5 to Lua 4:

--Linear Congruential Generatorfunction lcg(s, r)local temp ={}function temp:random(a, b)local y = mod(self.a * self.x + self.c, self.m)
 self.x = y
 ifnot a thenreturn y /65536elseifnot b thenif a ==0thenreturn y
 elsereturn1+ mod(y, a)endelsereturn a + mod(y, b - a +1)endendfunction temp:randomseed(s)ifnot s then
 s = seed()end
 self.x = mod(s,2147483648)endif r then--from Numerical Recipesif r =='nr'then
 temp.a =1664525
 temp.c =1013904223
 temp.m =65536--from MVCelseif r =='mvc'then
 temp.a =214013
 temp.c =2531011
 temp.m =65536end--from Ansi Celse
 temp.a =1103515245
 temp.c =12345
 temp.m =65536end
 temp:randomseed(s)return temp
end
print("wutwut =".. lcg(0):random())
print("wutwut =".. lcg(0):random())
print("wutwut =".. lcg(0):random())

However, the output is always the same number. What am I doing wrong?


Mike

AltStyle によって変換されたページ (->オリジナル) /