Question

Alright, someone must know easier ways to do this than me. I'm trying to write a random number generator using a fairly common formula.

--Random Number Generator
local X0=os.time() 
local A1=710425941047
local B1=813633012810
local M1=711719770602
local X1=(((A1*X0)+B1)%M1)

local X2=(((A1*X1)+B1)%M1) --then I basically take the vaiable X1 and feed 
                           --it back into itself.
print(X2)
local X3=(((A1*X2)+B1)%M1)
print(X3)
local X4=(((A1*X3)+B1)%M1)
print(X4)
local X5=(((A1*X4)+B1)%M1)
print(X5)
local X6=(((A1*X5)+B1)%M1)
print(X6)
local X7=(((A1*X6)+B1)%M1)
print(X7)

Etc Etc. Does anybody know a faster way to do this? I would love to be able to fit it into something along the lines of a:

for i=1,Number do
    local X[loop count]=(((A1*X[Loop count-1])+B1)%M1)
    math.randomseed(X[loop count])
    local roll=math.random(1,20)
    print("You rolled a "..roll)
end
io.read()

Type of string. I'm using it to generate random numbers for pieces of track I'm making in a tabletop game. Example hunk of code:

if trackclass == "S" then
for i=1,S do     --Stated earlier that S=25
    local roll=math.random(1,5)
    local SP=math.random(1,3)
    local Count= roll
    if Count == 1 then
        local Track = "Straightaway"
        p(Track.." Of SP "..SP)
        else
    end
    if Count == 2 then
        local Track = "Curve"
        p(Track.." of SP "..SP)
        else
    end
    if Count == 3 then
        local Track = "Hill"
        p(Track.." of SP "..SP)
        else
    end
    if Count == 4 then
        local Track = "Water"
        p(Track.." of SP "..SP)
        else
    end
    if Count == 5 then
        local Track = "Jump"
        p(Track.." of SP "..SP)
        else
    end
end

end

Unfortunately this seems to generate a pretty poor set of random number distribution when I use it and I would really like it to work out better. Any possible assistance in fleshing out the variable loop cycle would be greatly appreciated. Even something like a call so that every time math.random() is called, it adds one to the X[loop count]] so that every generated number is actually a better pseudo-random number distribution.

Please forgive my semi-rambling. My mind is not necessarily thinking in order right now.

Was it helpful?

Solution

Does anybody know a faster way to do this?

Each XN in the expression is always the previous X, so just restructure the code to use the previous X rather than creating new ones:

local X = os.time() 
local A1 = 710425941047
local B1 = 813633012810
local M1 = 711719770602

function myrandomseed(val)
    X = val
end

function myrandom()
    X = (A1 * X + B1) % M1
    return X
end

Now you can call myrandom to your heart's content:

for i=1,100 do
    print(myrandom())
end

Another way of packaging it, to avoid static scope, would be generating random number generators as closures, which bind to their state variables:

function getrandom(seed)
    local X = seed or os.time()
    local A1 = 710425941047
    local B1 = 813633012810
    local M1 = 711719770602
    return function()
        X = (A1 * X + B1) % M1
        return X
    end
end

Now you call getrandom to get a random number generator for a given seed:

local rand = getrandom()
for i=1,100 do
    print(rand())
end

I would love to be able to fit it into something along the lines of a: math.randomseed(X[loop count]) local roll=math.random(1,20)

If you're calling randomseed every time you call random, you're not using Lua's (i.e. C's) random number generator at all. You can see why this is true by looking at myrandomseed above. Why are you funneling your numbers through Lua's random in the first place? Why not just use math.random and be done with it.

Just make to sure to call math.randomseed once rather than every time you call math.random and you'll be fine.


I'm using it to generate random numbers for pieces of track I'm making in a tabletop game. Example hunk of code:

When you see tons of nearly identical code, you should refactor it. When you see variables names like foo1, foo2, etc. you're either naming variables poorly or should be using a list. In your case you have a bunch of branches Count == 1, Count == 2, etc. when we could be using a list. For instance, this does the same thing as your code:

local trackTypes = { 'Straightaway', 'Curve', 'Hill', 'Water', 'Jump' }
for i=1,S do 
    local trackTypeIndex = math.random(1, #trackTypes)
    local SP = math.random(1, 3)
    p(trackTypes[trackTypeIndex]..' of SP '..SP)
end

Note that you can probably guess what trackTypes is just by reading the variable name. I have no idea what S and SP are. They are probably not good names.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top