Weighting Random Values / Probability Distribution in VEX

   6397   4   0
User Avatar
Member
31 posts
Joined: July 2015
Offline
I'm attempting to create a way to intuitively weight how I randomly distribute assets via copy to points as well as what shaders are assigned via redshift's shader switch. I was able to successfully get this all to work with the standard rand() function but quickly realized the even distribution wasn't going to work for me. What I was able to come up with so far seems to work at first but if the values are close to equally weighted it begins to fall apart. My math is not the strongest which is why I have every variable output as an attribute to check what it's doing but I'm currently at a loss as to how to go about doing this.

Here's what i'm putting into my wrangle :

f@choiceA = chf(“A_Probability”);
f@choiceB = chf(“B_Probability”);
f@choiceC = chf(“C_Probability”);

f@max = @choiceA + @choiceB + @choiceC;

f@choiceA_percent = @choiceA / @max;
f@choiceB_percent = @choiceB / @max;
f@choiceC_percent = @choiceC / @max;

int seed = 2456;

f@randomNumber = rand(@ptnum + seed);

i@choice = 0;

if(@randomNumber < @choiceA_percent){
@choice = 0;
}

if(@randomNumber < @choiceB_percent){
@choice = 1;
}

if(@randomNumber < @choiceC_percent){
@choice = 2;
}

Attachments:
probabilityDistribution.jpg (1.8 MB)

User Avatar
Member
502 posts
Joined: July 2005
Online
Hi,

you are on the right way, but one thing is missing.

If you want to handle all the weights in an equal way, you have to accumulate them. A way to do this, is storing them into an array and normalize them (1 should me max).

You'll get something like this: (0.1, 0.25, 0.6, 1.0).

If you generate a random number, it will be between 0 and 1.
For example if the random number is 0.05, the index will be 0,
and if the random number is 0.5, the index is 2.

Here is an example.
Edited by Aizatulin - June 30, 2019 02:18:54

Attachments:
distribute_color_multi.hipnc (75.2 KB)

User Avatar
Member
31 posts
Joined: July 2015
Offline
Amazing. This is exactly what I was looking to be able to do. I don't fully understand what you're doing here but thank you for the example file and commenting each section. Cheers!
User Avatar
Member
9249 posts
Joined: July 2007
Offline
you can also directly use sample_discrete() function to get random color using weights
Tomas Slancik
CG Supervisor
Framestore, NY
User Avatar
Member
502 posts
Joined: July 2005
Online
If interested, here is a version using the inbuild sample_discrete() function, which is doing quite the same but with less code.

Attachments:
distribute_color_multiX.hipnc (75.0 KB)

  • Quick Links