Weighted Chance
#1
Posted 18 December 2010 - 05:36 AM
So say I have three different outcomes that I want to randomly choose from. Normally I use the choose() function or give each outcome a number and use the random() function to give me a number. The problem comes in when I want one of the particular outcomes to have a better chance of being chosen than another. How do I do this?
#2
Posted 18 December 2010 - 05:45 AM
Say you have three outcomes (A, B, and C). You want A and B to have a 25% chance of occuring, and C to have a 50% chance. To implement this you could do something like:
var_random_result=random(1);
if(var_random_result<0.25)
{
//execute outcome A.
}
else if (var_random_result<0.5)
{
//execute outcome B.
}
else
{
//execute outcome C.
}
Edited by LaLaLa, 18 December 2010 - 05:48 AM.
#3
Posted 18 December 2010 - 05:53 AM
The easiest way to do this is to assign different ranges to each outcome. You will need to use some simple alegbra; for example:
Say you have three outcomes (A, B, and C). You want A and B to have a 25% chance of occuring, and C to have a 50% chance. To implement this you could do something like:var_random_result=random(1); if(var_random_result<0.25) { //execute outcome A. } else if (var_random_result<0.5) { //execute outcome B. } else { //execute outcome C. }
But won't a result of 0.25 or less execute both outcomes of A and C?
#4
Posted 18 December 2010 - 06:42 AM
No, because it first gets a random number. From 0~1, as a decimal. Let's say it's 0.7.But won't a result of 0.25 or less execute both outcomes of A and C?
Check if it's less than 0.25. This is the first 25%.
Now, we bypass that and move on (else if statement) to if it's less than 0.5. This is the next 25%, because if it didn't apply to 0.25, then the next amount will be 0.5.
Then an else statement (Which means it will only now execute this one), the other 50% goes to the last option.
This method is a little tough to code in without realization though, so be careful with the percentages and ratios.
#5
Posted 18 December 2010 - 08:57 AM
// chance(arg0, arg1 ... arg15) // select random depending on diffrent amount of chance var sum,i;sum=0; for(i=0;i<16;i+=1) sum+=argument[i]; sum=random(sum) for(i=0;sum>argument[i];i+=1) sum-=argument[i]; return i;
just to explain how this work, first it sum them up
sum = 3 + 4 + 6 // total sum of 13
then select a random value from the total
sum = random(sum) // lets say it becomes 5
then it go thru all of them til it find the one it is smaller then, if it is not smaller then that chance is taken away and the next one is compared, and it is supposed to return the argument number
if sum<=3 return 0; // it will return 0 since it was the first one sum-=3 // sum is now 2 if sum<=4 return 1: // and this one is returned sum-=4 // and so onjust that I'm using a loop to add all the arguments, and then a loop to remove them from a sum as long as they are greater, when that loop ends then i will be the argument number you need
example:
select = chance(2,1,3,4) obj[0]=item_1; // 2/10 th chance obj[1]=item_2; // 1/10 th chance obj[2]=item_3; // 3/10 th chance if select!=3 // 4/10 th chance to not drop instance_create(x,y,obj[select])
Edited by FoxInABox, 18 December 2010 - 09:02 AM.
#6
Posted 30 January 2011 - 07:47 PM
Edited by Schae, 30 January 2011 - 07:47 PM.
#7
Posted 30 January 2011 - 10:28 PM
create event:
choice=0 choice=choose(0,1,2)step event:
if choice==0
{
// 1st A statement
}
if choice==1
{
// 2nd B statement
}
if choice==2
{
// 3rd C statement
}
Edited by mrsmes, 30 January 2011 - 10:29 PM.
#8
Posted 30 January 2011 - 10:43 PM
And this is an old post that was accidently bumped, so just let it fall back..
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users











