GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2009-07-30 15:18:37

Pixelated_Pope
Member
Registered: 2009-07-30
Posts: 24

exp_dist(x)

I am trying to generate a random number between 0 and 10 that is more likely to be 0 than 10.  the exp_dist() script seemed to be the key, but the upper bound argument that is passed in when calling the script isn't being adhered to. 
I ran the function as follows

currentChoice = floor(exp_dist(10));

So I was surprised to see that I was commonly seeing values over 10 and upwards of 108. 

Other than that it seemed to be doing exactly what I was expecting.  So I modified the script to divide the return value by argument0.

Am I simply misunderstanding the intended use for exp_dist() or is this a bug? 

And if I am misunderstanding, is there a better way to do what I'm doing?  (Sorry if this is in the wrong section, this is my first post)

Offline

#2 2009-07-31 01:13:21

xot
Administrator
Registered: 2007-08-18
Posts: 1,201

Re: exp_dist(x)

It seems the script doesn't do what it says. It's also a little vague. I can't be entirely sure of the author's intentent and I don't have what you'd call strong math skills.

Would this be more appropriate?

[tex]f(m; \lambda) = mR(1)^\lambda[/tex]

where m is the range (0 <= x < m) and R(n) is the random function.

//  random_exp(range, lambda)
return argument0 * power(random(1), argument1);

Something else you might try is to filter random(1) through a bias function:

http://www.gmlscripts.com/script/bias_fast

n = range * bias(b, random(1));

Or:

//  random_bias(range, b)
var r; 
r = random(1);
return argument0 * r / ((1 / argument1 - 2) * (1 - r) + 1);

Abusing forum power since 1986.

Offline

#3 2009-07-31 11:18:21

Pixelated_Pope
Member
Registered: 2009-07-30
Posts: 24

Re: exp_dist(x)

I have even less of what you might call "strong math skills"  or, at least, my math skills have been out of use so long they are practically useless.

I was able to get the exp_dist to work perfectly for my needs (with a bit of patching for numbers that uncommonly appear out of range), but I'll play with those other functions you mentioned to see if they work better and with less patching.

Thanks for the help and the reply.  Hopefully I'll be hanging around here more often.  I love the site, and as a new Game Maker user, I'll have lots of questions that I hope aren't too annoying.

Offline

#4 2009-07-31 16:56:02

xot
Administrator
Registered: 2007-08-18
Posts: 1,201

Re: exp_dist(x)

Welcome to site, I hope you continue to enjoy it, and welcome to Game Maker. I hope you find it as enjoyable as I do.

I see you are a new member at GMC. That's certainly your best bet for quickly getting answers to your questions. GMC has a very knowledgeable and very active membership. Of course, you are free to post GML-related questions in GMLjoint's Code Help forum if you like. Just don't expect the same lightning response.

http://www.gmlscripts.com/forums/viewforum.php?id=8

When you get the time and inclination, why not introduce yourself to the rest of the community?

http://www.gmlscripts.com/forums/viewtopic.php?id=4


Abusing forum power since 1986.

Offline

#5 2009-07-31 17:04:19

xot
Administrator
Registered: 2007-08-18
Posts: 1,201

Re: exp_dist(x)

Now the question remains: what to do about this script?

http://www.gmlscripts.com/script/exp_dist

/*
**  Usage:
**      exp_dist(x)
**
**  Arguments:
**      x       upper bound of desired random value
**  Returns:
**      a random number from 0 to x with an exponential ditribution
**
**  GMLscripts.com
*/
{
    return argument0 * (-ln(random(1)))
}

Obviously, I didn't do a good job of testing this. Clearly numbers outside of the intended range can be returned (and often are). What does the function actually do? Does that match the name? Or the description? The script needs fixing ... but what part?

I don't know.

For now, I'll mark it "under review" with a link here.


Abusing forum power since 1986.

Offline

#6 2009-07-31 17:19:21

Pixelated_Pope
Member
Registered: 2009-07-30
Posts: 24

Re: exp_dist(x)

Well, like I said, with a little revision, it actually functions very well.  Giving it a range of 0-10 I was getting zero 64% of the time and it curved nicely down to 10 which was less than .00% of the time.  (about 1/32000).  Anything outside of 10 was even more rare (obviously) but it was possible.  Although it's a little messier, to get it to function as it describes, here's what I did:

/*
**  Usage:
**      exp_dist(x)
**
**  Arguments:
**      x       upper bound of desired random value
**  Returns:
**      a random number from 0 to x with an exponential ditribution
**
**  GMLscripts.com
*/
{
    return ((argument0 * (-ln(random(1))))/argument0)
}

If you run the original script like this: exp_dist(10) will commonly get numbers nearing 100. 
So, as I described above, when you take the original return value and divide it by argument0, you start to get numbers closer to your original range.

And then to actually use it I included a while loop that would get me a new number as long as what was returned was outside of my range.  There's probably a better way to do that, though...

Offline

#7 2009-07-31 17:35:28

paul23
Member
Registered: 2007-10-17
Posts: 110

Re: exp_dist(x)

Uhm, well my first impression when I saw the above script was:

"why ln"? - Why taking the natural logarithm from the random number?

Why taking the logarithm in the first place, and why the NATURAL logarithm (what reason is there for that base?)..

With this division your "distibution" will simply be logarithmic..

Also the last posted script has the idea: a* f /a.. One could even forget about "argument0"  then at all?

The script looks a bit "randomly", looks like somebody put some functions together and saw interesting results....

Offline

#8 2013-08-23 20:55:56

brac37
Member
Registered: 2010-02-12
Posts: 18

Re: exp_dist(x)

On http://en.wikipedia.org/wiki/Exponential_distribution, I read that the exponential distribution gives numbers from 0 to infinity in any case, i.e. the distribution is not bounded. Its  probability density function is r.exp(-rx), where r is the rate parameter.

For the cumulative distribution function, we have y = 1 - exp(-rx). This is equivalent to x = -ln(1-y)/r, so I would suggest to divide by argument0 instead of to multiply.

If you think about it, the larger the rate, the faster the density function goes to zero when x gets larger, the fewer values that exceed some bound. So it must indeed be a downscale.

EDIT: see also http://stackoverflow.com/questions/2106 … stribution. So the current form is used as well.

Last edited by brac37 (2013-08-23 21:38:49)

Offline

Board footer

Powered by FluxBB