How can I weight the probability a specific value is returned by random/choose?

0 favourites
  • 4 posts
  • Just say I want to set a variable to a randomly chosen value of either 0, 1 or 2.

    I can use the action choose(0,1,2) In this case values 0,1,2 have equal probability of being returned.

    If the action is altered to be choose(0,0,1,2). Now the value 0 has twice as much chance as being returned as either 1 or 2.

    choose(0,0,0,1,1,2). In this case 0 has more chance of being returned than 1 or 2. And value 1 has more chance of being returned than 2, but less than 0.

    Is there a more elegant way to create this situation?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Use floor(random(100)) to set a local variable to a random number from 0-99 then adjust each possible outcome to be within a range of values that act as percentage i.e. if var is between 0 and 9 then that's a 10% chance.

  • ok I can see that would definitely work. But it would actually involve more steps that simply putting choose(0,0,0,1,1,2).

    I guess Im hoping to figure out a mathematical solution where I could weight the values within a single expression... Im not sure if its possible.

    So in non-programmer terms Im trying to say something like:

    On Event > Set Variable: (50% of the time return = 0, 30% of time return = 1, 20% of the time return = 2)

    Without the need to have additional conditions.. if that makes sense.


    And beyond this, Id like to have a solution that could be adapted in other instances to allow for more possible values. Like... in some other cases I might want to return values of 0,1,2,3,4... each with different probabilities.

  • I don't really see a problem as it's always a single event with sub events. If you want to make it reusable you can create some kind of function where you call and the parameters are new percentages or possible values to be chosen.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)