# Weighted probabilities

Get help using Construct 2

### » Mon Jan 23, 2012 2:16 am

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])

-- cacotigon
B
31
S
6
G
8
Posts: 232
Reputation: 6,144

### » Mon Jan 23, 2012 2:57 am

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".
B
82
S
26
G
63
Posts: 4,844
Reputation: 45,728

### » Tue Jan 24, 2012 11:47 pm

Hey R0J0hound, thanks for the link. I went ahead and implemented that in my code. Works great!
B
31
S
6
G
8
Posts: 232
Reputation: 6,144

### » Thu Feb 16, 2012 12:58 am

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.ranma2012-02-16 10:50:43
B
81
S
50
G
10
Posts: 555
Reputation: 13,039

### » Wed Apr 03, 2013 5:05 pm

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

B
13
S
2
Posts: 17
Reputation: 1,096

### » Wed Apr 10, 2013 8:16 am

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?
B
4
Posts: 5
Reputation: 735

### » Wed Mar 09, 2016 4:01 pm

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

weightedPropability.capx
disConstruct, a Discord server that's all about Construct 2 !!!
Promote your Construct2 related content, hang out, get feetback, learn some tips & tricks and share your knowledge!

https://discord.gg/0qgSPOo3EsYRQOFt
B
60
S
26
G
6
Posts: 78
Reputation: 6,596

### Who is online

Users browsing this forum: Maverick1912, odiusfly and 20 guests