Random without math.random

Hi.
In my last posted discussion i used my own random function (because i don’t want to initiate standard function from math lib), so today i want to describe it.
My function uses os.time() as seed.
My function code:

initial global a > 0

function calla()
    a = math.fmod(math.sqrt(os.time()), a+2)
    while a>1 do
        a = math.fmod(math.sqrt(os.time()), a+1)
    end
    return a
end

In next images i uses white dots to represent density of possibility, white lines as n*0.2 marks(where n=1 to 5), blue dots to represent integral average value, and multi-color rectengles to represet count of random values(results of call calla, or math.random()) that are fill current interval of partition(i divided segment from 0 to 1 in 500 intervals to do this images).
Standard function(math.random() with math.randomseed(os.time())):
http://s14.postimg.org/pa0tg7a7l/image.jpg
My function:
http://s14.postimg.org/667hzuxdd/image.jpg
As you can see thay look almost the same, and have grate random properties (one higher because i take screensots in not exact same time when code was runing).
So you are free to use it in your projects).
And finally code that generate this images:

function setup()
    displayMode(FULLSCREEN)
    s = image(WIDTH, HEIGHT)
    a = 10 --use any value biger than 0 
    sm = 0
    t = {}
    count = 500
    for i = 1, count do 
        table.insert(t, 0)
    end
    print(calla(), #t)
    steps = count
    am =0
    k = 0
    rm = 0
    math.randomseed(os.time())
end

function draw()
    w = WIDTH/#t
    for i =1, 1000 do
        k = math.floor(calla()*steps)
        if k<=#t and k>0 then
            t[k] = t[k] + 1
        end
        rm = rm + 1
    end
    setContext(s)
    fill(0,25)
    rect(0,0,WIDTH,HEIGHT)
    stroke(255)
    strokeWidth(2)
    for i = 1, math.floor(#t/100) do
        line(100*i*w, 0, 100*i*w, HEIGHT)
    end
    strokeWidth(0)
    local cm,tm = 0,0
    for k,v in pairs(t) do
        if v>0 then
            fill(255*(1-k/#t)^(1/3), 255*(k/#t)^(1/3), 0)
            rect(w*(k-1)+1,0,w, v)
        end
        cm = cm + v
        fill(255)
        ellipse(w*(k-1)+w/2, HEIGHT*cm/rm, w+1)
        fill(0,0,196)
        tm = tm + v
        if k>1 and k<#t then
            ellipse(w*(k-1)+w/2, tm/k, w+1)
        end
    end
    if am <#t-1 then
        am = am + 1
    else
        am = 1
    end
    setContext()
    sprite(s, WIDTH/2, HEIGHT/2)
end

function calla()
    a = math.fmod(math.sqrt(os.time()), a+2)
    while a>1 do
    a = math.fmod(math.sqrt(os.time()), a+1)
    end
    return a
end

Why wouldn’t you use math.random? Surely there are far more interesting things to do in Codea than rewrite built in functions? :smiley:

I’ve always wondered how psuedo-random numbers are generated though, can you explain the idea behind the code?

Also as Ignatz asked why don’t you want to use the built in function?

It is extremely difficult to generate a good RNG, just looking random is nowhere near enough - google “test random number generator” to see the kind of tests required.

It’s like cryptography - homemade versions almost always have flaws, so it’s much, much, much, much better to stay with fully tested algorithms.