Math.Random question

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Math.Random question

Post by Inny »

Robin's explanation was better than my own, but I'll expand upon it. The idea of something being "random" means that it's suitably unpredictable. But, there are certain probabilistic properties that come out of looking at a lot of numbers that came out of the random number generator.

Lets try this list: 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1.

Is that random? Well, you can say it is random, because it's unpredictable where the 3s and 2s appear, but since the 1s appear so common, it's unsuitable. We call this "Biased" or "Weighted." So, for a proper random number generator to be usable, it needs to not be biased, meaning that all numbers have the same chance of showing up as any other.

Now, lets try this: -4 4 1 3 -5 -5 -5 -5 5 -3 5 4 -5 -4 2 4 5 4 2 -1

If you add those all together, you get 2, meaning that you didn't travel very far from the start. So, here's the core problem: You're misunderstanding what random really is. If you keep doing random events over and over and over, they eventually even out because it has to, otherwise it would be biased and no good to you. The period before it evens out is really really long, but the effects start to become noticeable soon enough.

So again, the tip: Use it less. Generate the random number once, and use it for a bit of time before getting another one.

Also, when I said cosmic radiation, I was being half-facetious. Home computers don't have a way to detect cosmic radiation. Government and Academic science labs, and the brains of cats, are where you detect cosmic radiation and get truly random events.
Luska72
Prole
Posts: 18
Joined: Thu Jan 03, 2013 6:14 pm

Re: Math.Random question

Post by Luska72 »

Oh, ok. Thank you very much for teaching me that :)

I will read everything linked, hopefully I'll get this working in no time!
User avatar
interstellarDAVE
Prole
Posts: 23
Joined: Mon Jan 14, 2013 8:22 pm

Re: Math.Random question

Post by interstellarDAVE »

My recommendation would be to use random to give the object a destination and a speed (like others have mentioned here). Something like this.

Code: Select all


function npc:updatepath()
    object_destination_x = math.random(1,windowwidth)
    object_destination_y = math.random(1,windowheight)
    object_traveltime = math.random(minimum_time, maximum_time)
end
and have the object call it again whenever it reaches its destination so it keeps moving in a seemingly random path. You can even make it better by including the players location in there somehow, so that when the player is close enough the npc will try to move randomly away from the player instead of close to the player.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 58 guests