Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Randomness

NatNat
edited November 2011 in Code Sharing Posts: 143

I have needed multiple independent random number generators. Here's a class that does this by swapping the global seed around, and provides a nice API that hides all that malarkey. It should also solve the sound breaking randomness issue. You have to create Random objects before doing anything that affects the global seed.

Random = class()

function Random:init(seed)
    self.seed = seed or math.random(0, math.huge)
end

function Random:uniform()
    local savedSeed = math.random(0, math.huge)

    math.randomseed(self.seed)
    local rnd = math.random()
    self.seed = math.random(0, math.huge)
    math.randomseed(savedSeed)

    return rnd
end

function Random:gaussian(n)
    local diceRolls = n or 2
    local total = 0

    for i = 1,diceRolls do
        total = total + self:uniform()
    end
    return total / diceRolls
end

function Random:clone()
    return Random(self.seed)
end
Tagged:

Comments

  • NatNat
    Posts: 143

    Doh! Can't work out how to format the code properly. Sorry.

  • SimeonSimeon Admin Mod
    edited November 2011 Posts: 5,387

    Nice! Good class. We'll look at changing the Lua generator to a mersenne twister implementation in the future - to solve some of those bugs. But this class will always be handy.

    Edit: fixed your formatting.

  • Posts: 2,161

    Hmm, you might want to average a few more samples before you call it a "gaussian" distribution.

  • BortelsBortels Mod
    Posts: 1,557

    two samples is PLENTY.

    what could go wrong?

  • NatNat
    Posts: 143

    Ha!

    It's not a crypto strength RNG. For my needs (game scenery and particle systems) it just needs to produce a reasonable approximation of a normal distribution while minimising processing.

  • Posts: 437

    Thanks for the class :)

  • Posts: 2,161

    two samples is PLENTY.

    I really really hope that that was a joke.

    My understanding is that you need about 30 before the central limit theorem kicks in.

  • NatNat
    Posts: 143

    Ok, I've edited it. How does it look now? You can now call random:gaussian(30) to get the all the smooth gaussian goodness you want, at the expense of compute time.

    Seriously, for deciding the distribution of stars in a space scene or particles emitted from a rocket engine 2 dice rolls works out fine.

  • BortelsBortels Mod
    Posts: 1,557

    Hee hee - I can see trolling Andrew on math stuff will be a fun and exciting topic :-)

    Yes, of course, a joke. And, as Nat points out, true if your use case is "rocket engine particles that I need to work most of the time".

    (True story: I WAS a math major at one point. One day in my probability class, I looked up and realized that not only did I not understand what was on the board - I hadn't understood it for at least three weeks. Stood up in the middle of class, gathered my stuff and walked out silently with all eyes on me - across campus to the CS department. "I'm sorry - CS is an impacted major, there's a waiting list based on seniority" "I have 186 units." "Welcome to CS!". Moral of the story: Math is Hard.)

  • Math is hard, I'm going to have to ask Andrew about a beginners guide to be able to attempt a few more basic 3D functions. Possibly the 3D cube sample Bortels posted is all I need to consider.

    I was able to create the vanshing point sample. While that is technically math I remember it more from 9th grade art class than acutal math. It is like having a still window (or painting) you can change your vantage point in. The next jump is to be able to rotate this windows's (painting's) point of view.

  • BortelsBortels Mod
    Posts: 1,557

    Relevant to 3d and math is hard - this page has THE BEST simple explanation of projecting 3d coordinates onto a 2d plane I've found - one diagram, as simple as it needs to be but no simpler. Seriously. There's a link to all sorts of other good stuff about software-only 3d rendering as well.

    http://semantix.gr/statueJS.html

  • Ha, I did it backwards. I'm not sure what I may be missing by not using focal length.

  • Posts: 2,161

    Curiously, I've been thinking a bit about 3D stuff recently ...

    Anyway, I agree that averaging two uniform distributions is good enough for when you want to generate a bit of noise in a picture, just don't call it a Gaussian and I'll be happy.

Sign In or Register to comment.