October 23rd, 2005, 01:30 AM
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.
October 23rd, 2005, 04:15 AM
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)
October 23rd, 2005, 07:31 PM
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.
October 23rd, 2005, 10:56 PM
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.
October 31st, 2005, 05:11 PM
Thanks for your help. I'm about half-way there.
October 29th, 2009, 09:52 AM
how about with Gaussian Bell Function probability distribution?
Originally Posted by Lux Perpetua
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.
Thanks in advance,
October 30th, 2009, 04:18 PM
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.
November 12th, 2009, 02:50 AM
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.)
Originally Posted by Farzan Yazdi