Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
2.80/5 (2 votes)
See more:
How can i set a % chance of getting a certain output. Like this:

I have game where i can find some items. The item can have Max Life e.g.

The Max Max Life is 200 and the minimum max life is 1.

When i generate the max life I use random.next(1, 201) now to get a random output between 1 and 200. But I want to make 50% of the items Max Life less than 100 max life, and only 1% of the items Max Life to be 200.

How can i do that?
Posted
Comments
Richard MacCutchan 8-Dec-10 9:21am    
What happens to the other 49%? You just need to set a different randomiser or counter to decide which set to select from.
Manfred Rudolf Bihy 8-Dec-10 9:45am    
Now that's strange you want to have a higher probability for 200 than would be expected from an even distribution. When you have numbers from 1 up to 200 evenly distributed that would mean every number has chance of 0.5% of coming up, and you want to double that for number 200 to 1.0%. What are you up to. Explainez vouz! :)
wizardzz 8-Dec-10 10:08am    
Just to clarify, you want 1-99 = 50% 100-199 = 49% and 200 = 1%?
Toli Cuturicu 8-Dec-10 11:33am    
Not even remotely clear.

You're request isn't very clear. However, suppose you have your own probability distribution for lifes:


lifesprobabilityprob. density
1 - 9950%0.00505
100 - 19949%0.00490
200 - 2001%0.01005


Where third column holds discrete densities of probability
(note I've arbitrarly boosted number 200 density to make cumulative sum be 1, as required).

Now, if you extract a random number with uniform probability (as Random does) you've to equate the cumulative probability functions to get the corrensponding number of your distribution.
Since the granularity of our densities is 10^-5, we can choose the interval 0-99999 for Random and use only integer numbers, this way:

Random outcomechoosen life
0-5041
505-10102
.....
49490-4999499
49995-50484100
50485-50974101
.....
98505-98994199
98995-99999200


Implementing such logic is almost trivial:

C#
public static int myNext(Random r)
{
    n = r.Next(100000);
    if (n < 49995)
    {
        return 1 + (n / 505);
    }
    else if (n < 98995)
    {
        return 100 + ((n - 49995) / 490);
    }
    else
    {
        return 200;
    }
}



:)
 
Share this answer
 
v2
Comments
wizardzz 9-Dec-10 11:22am    
Good call, that's a better explanation than msdn provides for distribution proportions: http://msdn.microsoft.com/en-us/library/9b3ta19y.aspx
(Second example)
CPallini 10-Jan-11 17:51pm    
Thank you.
If what I specified above it true, you can generate random numbers twice.
First do a random.next(1, 101).

If it returns 1-50 then you are in the under 100 selection and then do random.next(1, 100) to get a number between 1 and 100 for your Life value.

If it return of 51-99 then you are in the 100-199 selection, so do a random.next(101, 200) to get your Life value.

If the initial random returns 100, then your Life value is 200.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900