Weighted probabilities

0 favourites
  • 8 posts
  • Hi everybody,

    I'm having some trouble figuring out how to create a set of weighted probabilities easily in the event code. Let me outline what I have:

    if(EntityA is overlapping Trigger)

         Set EntityA.ExecuteSub = true; // this is so EntityA only runs the overlap code a single time.

         System.Create(EntityA)

         Set EntityA Animation (Choose("Animation1", "Animation2", etc))

         Set EntityA size

         Set EntityA Position

         Set EntityA localvar1

         Set EntityA localvar2

         Set EntityA localvar3

         Set EntityA localvar4

         Set EntityA Speed

         { ... more instructions involving EntityA }

    I have ten different animations in Entity A and it randomly selects one each time. However, now I want to weight the probability so certain animations are more likely to appear than others.

    I planned on setting a global variable called RandomNumber, and than basically being like:

    if(RandomNumber < 10) Choose (EntityA.Animation1)

    else if(RandomNumber < 25) Choose (EntityA.Animation2)

    else if(RandomNumber < 30) Choose (EntityA.Animation3)

    I have a feeling I'm going to have to create a new global boolean variable called RandomNumberMatched and then:

    0. Check primary condition overlapping

    1. Generate random number

    2. Spawn EntityA - and hope that the following subevents automatically refers to this one

    3. Write a bunch of Subevents under the primary event checking the Random number value as follows:

    Sub event:

    if (RandomNumber < 10)

         RandomNumberMatched = true

         EntityA.SetAnimation( EntityA.Animation1 )

    if (RandomNumber < 25)

         RandomNumberMatched = true

         EntityA.SetAnimation( EntityA.Animation2 )

    if (RandomNumber < 30)

         RandomNumberMatched = true

         EntityA.SetAnimation( EntityA.Animation3 )

    4. Rerun the rest of the set size,position,etc etc on this EntityA *AFTER* the subevent code has finished I guess putting the entirety of this code in a blank sub-event on the primary event.

    Without an if-elseif-else condition I have to use an additional variable to falsify the following subevents.

    I really hope there's a more modular way to encapsulate what would ordinarily be a fairly and I would think common thing to do in conventional OO coding.

    I wish I could just write a function

    GetWeightedString( ProbabilityArray, StringArray )

    which takes a list of numbers up to 100 returns the respective String in the array, and then I could just call a simple function that would Spawn based on the Object's name.

    Something like this:

    Spawn randomentity = GetWeightedString(new Array[2, 4, 8, 20, 25, 60, 80, 100], new String[EntityA, EntityB, EntityC, EntityD, EntityE, EntityF, EntityG, EntityH])

    Thanks everybody for your assistance!

    -- cacotigon

  • I found this site which has a fairly simple approch to doing weighted random.

    http://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/

    and implemented in c2:

    http://dl.dropbox.com/u/5426011/examples%208/weighted_random.capx

    It can be made into a function fairly easily by using the "function plugin".

  • Hey R0J0hound, thanks for the link. I went ahead and implemented that in my code. Works great!

  • just wanted you to know, this capx by R0J0hound:

    http://dl.dropbox.com/u/5426011/examples%208/weighted_random.capx

    saved me a lot lot lot of time.

  • Thank you so much for posting the CapX.

    I'm linking to a tweaked version that I did - mind you i suck at programming - but I wanted to share what I tweaked (basically to use in a function) - in case it helped someone else...

    Thanks

    docs.google.com/file/d/0BzelBqNJ0DazS0JBZ2UwdHktZEE/edit

  • I tried the capx file but it always seems to return the last of the values when I run the layout.

    Is there something wrong with the code?

  • I mad a tweaked version too and build a small use case example for it.

    weightedPropability.capx

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • WOW!!!

    Thank you soooo much and R0J0hound

    I was about to code my own weighted RNG from scratch. You two just saved me a lot of hours!!!

    +1

    Thumbs Up!

    Like!

    Vote Up!

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