Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
October 23rd, 2005, 02:30 AM
 _jameshales
Registered User

Join Date: Oct 2005
Location: Perth, Western Australia
Posts: 6
Time spent in forums: 1 h 35 m 7 sec
Reputation Power: 0
Weighted Random Theory

Does anyone know of any links to the theories behind generating weighted numbers? Particularly I want to be able to generate a random floating point number between 0 and 1. But I want it so that the likely hood of each number between 0 and 1 appearing, can be predicted with a simple equation, i.e. that produces a parabola, so that maybe the numbers near the middle are more likely to appear, or the numbers on one side, etc.

It's just that I've found plenty on weighted random choices, but those are usually finite number of choices. I'm using this for a game where each player has a "luck" value. Basically I want this parabola of probability to move from one side of the spectrum, i.e. 0, to the other, so that as your luck increases, it's more likely that it'll produce a 1. Up until now I've simply been taking a random number, dividing it by the luck value, and taking that from one (so it goes closer to one as luck increases), but that completely takes away the possibility of a lower number appearing, rather than simply making it less likely.

Thanks in advance for any help.

#2
October 23rd, 2005, 05:15 AM
 Learnable
Registered User

Join Date: Oct 2005
Posts: 1
Time spent in forums: 2 h 39 m 7 sec
Reputation Power: 0
Probability

Why can't you use the luck value as a probability which increases or decreases according to your function, and then:

float n = Random();
if(n < luck)
{}
else
{}

#3
October 23rd, 2005, 08:31 PM
 _jameshales
Registered User

Join Date: Oct 2005
Location: Perth, Western Australia
Posts: 6
Time spent in forums: 1 h 35 m 7 sec
Reputation Power: 0
I wanted a number to come out of it. It's for an RPG style game, and the luck values will be used in some situations for decisions, but mostly for modifying values like hit damage and such, with a random degree of error - i.e. so that the final damage dealt could be 75% - 125% of the value that I had before. The random number, multiplied by 50% of the value, would be subtracted from the value, and a 25% of the value added back on, so that if the random number is 1, the value will equate to 125%, and if it's 0, the value will equate to 75%. The luck value should make one end of the spectrum more probable, so that a higher luck value would turn up more values closer to 1.

If we were to graph the frequencies of the random numbers from 1 to 0, it should form some sort of bump in the middle. As the luck value increases, that bump should move closer to the 1, so that 1's appear more often than before.

I found something about Gaussian random values that I think will work however I'm trying to investigate exactly how they work.

_jameshales

#4
October 23rd, 2005, 11:56 PM
 Lux Perpetua
Contributing User

Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,939
Time spent in forums: 1 Month 1 Week 3 h 27 m 29 sec
Reputation Power: 1312
So you want to define a probability density function f on the interval [0,1] that looks something like a parabola, and you want to generate random numbers according to this distribution? Here's one way to do it:

1) Find the indefinite integral from 0 to x of f, and call this function F.
2) Find the inverse of F. Call this function G.
3) Now you can simply generate a uniform random number x between 0 and 1 and return G(x) as your weighted random number.

For this to work, your density function f must of course be a valid probability distribution, meaning

A) It is never negative,
B) Its integral from 0 to 1 is equal to 1.

(If your f satisfies property A, then you can make it satisfy property B by scaling it.)

Of the three steps above, (1) and (2) could be more or less tricky depending on your density function. A parabola is very easy to integrate, but its indefinite integral is a cubic function, and although cubics can be inverted, the formula is quite complicated. There are ways to do it without finding the explicit inverse formula, though.

#5
October 31st, 2005, 06:11 PM
 _jameshales
Registered User

Join Date: Oct 2005
Location: Perth, Western Australia
Posts: 6
Time spent in forums: 1 h 35 m 7 sec
Reputation Power: 0

_jameshales

#6
October 29th, 2009, 10:52 AM
 Farzan Yazdi
Registered User

Join Date: Oct 2009
Posts: 1
Time spent in forums: 21 m 48 sec
Reputation Power: 0
how about with Gaussian Bell Function probability distribution?

Quote:
 Originally Posted by Lux Perpetua So you want to define a probability density function f on the interval [0,1] that looks something like a parabola, and you want to generate random numbers according to this distribution? Here's one way to do it: 1) Find the indefinite integral from 0 to x of f, and call this function F. 2) Find the inverse of F. Call this function G. 3) Now you can simply generate a uniform random number x between 0 and 1 and return G(x) as your weighted random number. For this to work, your density function f must of course be a valid probability distribution, meaning A) It is never negative, B) Its integral from 0 to 1 is equal to 1. (If your f satisfies property A, then you can make it satisfy property B by scaling it.) Of the three steps above, (1) and (2) could be more or less tricky depending on your density function. A parabola is very easy to integrate, but its indefinite integral is a cubic function, and although cubics can be inverted, the formula is quite complicated. There are ways to do it without finding the explicit inverse formula, though.

Hello,
I found your solution very interesting, unfortunately it is not 100% applicable for my problem. I have been searching for this for more than a week but I haven't found any reasonable solution!!! My problem is that I want to generate my random numbers according to a Gaussian Bell Function, let's say the standard one e^(-((x-2)^2)/2). But I just don't know how to calculate the indefinite integral of that. I've been googling for that and I found out that there are no indefinite integral existing for Gaussian Bell Function I just don't know what to do!!! I'd appreciate any help, recommendation or hints on this.

Farzan

#7
October 30th, 2009, 05:18 PM
 jwdonahue
Contributing User

Join Date: May 2004
Posts: 3,417
Time spent in forums: 3 Weeks 5 Days 12 h 51 m 9 sec
Reputation Power: 886
A very crude way to accomplish what you are trying to do is to fill a table with random numbers, then if your player has lots of good karma coming their way, throw out some number of the lowest or highest values (whichever is beneficial for the conext) and randomly pick one of the remaining values. To get the curve towards the middle you would throw out some number of highest and lowest values in the set and then randomly select one of what's left.
__________________
I no longer wish to be associated with this site.

#8
November 12th, 2009, 03:50 AM
 Lux Perpetua
Contributing User

Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,939
Time spent in forums: 1 Month 1 Week 3 h 27 m 29 sec
Reputation Power: 1312
Quote:
 Originally Posted by Farzan Yazdi My problem is that I want to generate my random numbers according to a Gaussian Bell Function, let's say the standard one e^(-((x-2)^2)/2). But I just don't know how to calculate the indefinite integral of that.
For good reason - it can't be done with elementary functions. The Box-Muller transform is an easy way to generate standard normal deviates. (The link gives you some alternatives and weighs some of the pros and cons.)

 Viewing: Dev Shed Forums > Programming Languages - More > Software Design > Weighted Random Theory