Jump to content


Photo

Weighted Chance


  • Please log in to reply
7 replies to this topic

#1 SHiLLySiT

SHiLLySiT

    GMC Member

  • New Member
  • 78 posts
  • Version:GM:Studio

Posted 18 December 2010 - 05:36 AM

This problem has appeared in other projects, but I found some way around it. Now, not only do I need this to work, but I really want to know how.

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?
  • 0

#2 LaLaLa

LaLaLa

    GMC Member

  • New Member
  • 511 posts
  • Version:GM8

Posted 18 December 2010 - 05:45 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.
}

Edited by LaLaLa, 18 December 2010 - 05:48 AM.

  • 0

#3 SHiLLySiT

SHiLLySiT

    GMC Member

  • New Member
  • 78 posts
  • Version:GM:Studio

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?
  • 0

#4 sigonasr

sigonasr

    Hard Code that Works

  • GMC Member
  • 412 posts
  • Version:GM8

Posted 18 December 2010 - 06:42 AM

But won't a result of 0.25 or less execute both outcomes of A and C?

No, because it first gets a random number. From 0~1, as a decimal. Let's say it's 0.7.

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.
  • 0

#5 FoxInABox

FoxInABox

    GMC Member

  • GMC Member
  • 5400 posts
  • Version:GM:Studio

Posted 18 December 2010 - 08:57 AM

Add a new script and call it chance, and put this code there:
// 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 on
just 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.

  • 0

#6 Schae

Schae

    GMC Member

  • New Member
  • 4 posts

Posted 30 January 2011 - 07:47 PM

i didnt mean to post here someone delete this lol

Edited by Schae, 30 January 2011 - 07:47 PM.

  • 0

#7 mrsmes

mrsmes

    GMC Member

  • GMC Member
  • 972 posts
  • Version:Unknown

Posted 30 January 2011 - 10:28 PM

or you could do a variable called choice,
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.

  • 0

#8 FoxInABox

FoxInABox

    GMC Member

  • GMC Member
  • 5400 posts
  • Version:GM:Studio

Posted 30 January 2011 - 10:43 PM

You didn't read the OP? the chance for them to happen is equaly distributed, he wanted a weighed chance, if you want to get somewhere close to that with choose then you have to use the same number in several of the arguments..

And this is an old post that was accidently bumped, so just let it fall back..
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users