lua-users home
lua-l archive

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


OK since this code is not working for me, does anyone have a
suggestion for a different PRNG implementation I could use instead?


Mike




On Mon, Sep 9, 2013 at 8:17 PM, Michael Horvath <mikh2161@gmail.com> wrote:
> 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 Generator
> function lcg(s, r)
>     local temp = {}
>     function temp:random(a, b)
>         local y = mod(self.a * self.x + self.c, self.m)
>         self.x = y
>         if not a then
>             return y / 65536
>         elseif not b then
>             if a == 0 then
>                 return y
>             else
>                 return 1 + mod(y, a)
>             end
>         else
>             return a + mod(y, b - a + 1)
>         end
>     end
>     function temp:randomseed(s)
>         if not s then
>             s = seed()
>         end
>         self.x = mod(s, 2147483648)
>     end
>     if r then
>         --from Numerical Recipes
>         if r == 'nr' then
>             temp.a = 1664525
>             temp.c = 1013904223
>             temp.m = 65536
>         --from MVC
>         elseif r == 'mvc' then
>             temp.a = 214013
>             temp.c = 2531011
>             temp.m = 65536
>         end
>     --from Ansi C
>     else
>         temp.a = 1103515245
>         temp.c = 12345
>         temp.m = 65536
>     end
>     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