Normal Distribution

Hey,
I was just wondering, how do you get a random value, with a normal distribution?

Ask Mr Google

https://github.com/torch/torch7/blob/master/doc/random.md
I’m still confused, how do you import the torch thing, the luarock et cetera didn’t work.

You don’t need a mersenne generator, keep looking

Like is it a simple command, or do I need to write a function to do it?

Function

Thought I’d give this a try.

displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    lim=30000
    create()
end

function create()
    tab={}
    for a=1,lim do
        -- create a random value with a normal distribution
        p=math.sqrt(-2*math.log(1-math.random()))
        x=p*math.cos(2*math.pi*math.random())   -- x = random value
        
        -- create a table showing the distribution of all the values
        v=(x*120)//1
        if tab[v]==nil then
            tab[v]=1
        else
            tab[v]=tab[v]+1
        end
    end
end

function draw()
    background(0)
    stroke(255)
    strokeWidth(2)
    for a,b in pairs(tab) do
        line(WIDTH/2+a,50,WIDTH/2+a,5*b+50)
    end
    stroke(255,0,0)
    strokeWidth(3)
    line(0,50,WIDTH,50)
    line(WIDTH/2,50,WIDTH/2,HEIGHT)
    fill(255)
    text("Graph showing normal distribution of  "..lim.."  random values.",WIDTH/2,HEIGHT-20)
    text("Tap restart icon for another distribution",WIDTH/2,HEIGHT-50)
end

Here’s another version showing the distribution of 30,000 points around the center of the screen.

EDIT: This shows a good example of what a globular star cluster looks like. See this link.

http://earthsky.org/clusters-nebulae-galaxies/omega-centauri-milky-ways-prize-star-cluster

displayMode(FULLSCREEN)

function setup()
    tab={}
    for a=1,30000 do
        p=math.sqrt(-2*math.log(1-math.random()))
        x=p*math.cos(2*math.pi*math.random())
        p=math.sqrt(-2*math.log(1-math.random()))
        y=p*math.cos(2*math.pi*math.random())
        table.insert(tab,vec2(x,y))
    end
end

function draw()
    background(0)
    fill(255)
    for a,b in pairs(tab) do
        ellipse(WIDTH/2+b.x*100,HEIGHT/2+b.y*100,3)
    end
end

@dave1707 Thanks! Can you explain the maths behind it? I was trying to do it using only one random value, but supposedly you can’t integrate e^-x^2. Does Lua have the error function?
EDIT: I’m going to try an approximation of the error function later, and I’ll send what I end up with.

@MattthewLXXIII I can’t explain the math. I did a google search for normal distribution of random numbers and found various formulas. I tried to find the link to the one that was the best, but I wasn’t able to find it again. I don’t remember the exact wording I used in the search. I’m not sure what error function you’re referring to, but I doubt if Lua has it.

@dave1707 thanks for your help.
@Andrew_Stacey can you help?

@dave1707 thanks for your help.
@Andrew_Stacey can you help?