Author Topic: Weighted Randomizing  (Read 1778 times)

Hi,
If I want to weight different values with a higher percentage in a function -- how would I go about that ?
So instead of completely random I want the values to be chosen in a weighted manner:

Value 1 = ~ 50%
Value 2 = ~ 20 %
Value 3 = ~ 20%
Value 4 = ~ 10%

So if I have a really unique element that is clearly identifiable as repeating I can keep its occurence to a reasonable minimum.
If I were to script this I would do a for loop but I don't see a way in the function graph to do loops.
Is it even possible to do this ?

Thanks,
Jens
Last Edit: April 18, 2016, 04:56:19 pm

Ok, not sure if I am doing this right.
In the top image each value hasa probability of 25% of being chosen right ?
In the bottom image by adding more uniform blends to preferred values I am increasing their chances to 30%.
So Subsequently I will need to add in a couple more steps to weight things differently.

Is this the way to do it or is there some easier (possibly built in) way ?

I'm not sure how that node works, I haven't used it. But here's what I'd do. Since you're converting to int, how I would do it is use ifs to select a value based on the range of a random number.
So, I'd have a float 1 -> random node, and then compare that to the various weights in if statements.

so here's some pseudocode.

vRand=Random (float(1))
if(vRand< float(0.5)){
   int=Value1
} else if (vRand<0.7){
  int=Value2
} else if (vRand<0.9){
  int=Value3
} else {
  int=Value4
}
 
So, set up those ifs with Less-Than comparisons, feed the less-thans the random function, and it should do what you want.

That makes sense, I give that a go.

Thanks!
Jens

Hm. Still not sure I am doing it right.
In my book Input 4 (green) should only have a roughly 5 percent chance of being picked.
Yet in a range from 0 - 1500 randomization I seem to be hitting green a lot.

I read the graph as:

- Generate random value of 0 - 1
- If Value less than 0.3 Output 1, else output random value
- If Value less than 0.6 Output 2, else output random value
- If Value less than 0.95 Output 3, else output random value
-If Value less than 0.99 Output 4, else output random value

I would almost need to figure out the equivalent of a 'break' statement, that if one if condition becomes true, that it stops and outputs the value.
Because at the moment it almost seems that even though conditions are met earlier in the chain the value (1,2,3) that is output doesn't get through the chain ?
This visual coding is confusing me, I want an expression node :)


EDIT: Tried as well feeding the the Output of the second to last if statement into the ELSE of the last if statement since I assumed that if any of the previous ifs returned a value greater than 1 (2,3) that I would get this value through the chain this way but no joy
Last Edit: April 19, 2016, 10:47:22 pm

The EDIT of my last post actually triggered the solution.
So this is working now. Thanks for the idea !


Glad you got it working! That's a way of doing it I hadn't come across before, but it looks like it works.
What I usually do is have my if statements feed directly into the next if's Else in a chain. So in your case, I'd put the gt:0.99 if leading to the else of the gt:0.95 if, which leads to the else of the gt:0.6 if... etc.

I think that's the best method for the 'break' functionality you were pondering, there.

Visual coding sure is a different experience... but it'll get pretty smooth with a bit of practice.

Oh I think we have the same way.
The if Statements are directly connected. Just a little awkward layout of the Lesser/Equal Nodes on top of the connection between the ifs.

Edit: Never mind. You are right. Yours would be different. I'll try that out as well just to get the idea.
Last Edit: April 23, 2016, 01:24:08 am