View Full Version : Weighted randomness
Hey
I am trying to figure out a very simple code to do some weighted randomness.
I have two values, and I have to randomly pick among them on each iteration.
But I need, let's say, the first value to be picked on 60% of the cases, and the second value to be picked on 40% of the cases.
Anyone can write me a pseudo code to do that?
I know it is simple, I learned it one day in my life, but right now I'm blank!
Thank u a lot
Daniel


trancor
07102007, 03:36 AM
1 value for 60% of cases? and 2nd value for 40%? Not entirely sure how you want this used or how you want it to play out.
rand(0,50);
This will pick between 0 and 50 a random value but will never pick 50 because it's out of 50.
I don't really know what you mean by cases, is it like an even interval that counts but you want something to evenly change the random value 60 out of 100 times?
int $caseInterval=Current number case;
int $maxValue=100;
int $intPercent=60;
float $minRandom=0;
float $maxRandom=50;
int $counter=((100$intPercent)/$maxValue);
if (($caseInterval % $counter) == 0) {
float $randNumber=rand($minRandom,$maxRandom);
}
int $caseInterval=Current number case;
Either your $i from a for loop or particle id or time/frame or what ever it is that is counting up.
int $maxValue=100;
The max number of cases
int $intPercent=60;
The percent at which you'd like the random number to change
float $minRandom=0;
float $maxRandom=50;
The minimum and maximum numbers for random to choose between
And simply change float to int for a interval or float on the random value in the if statement but you should know that.
I didn't know what you really wanted by your wording "case" and in what capacity you'd use this so the script is a bit sloppy. If you provide a little more info maybe I can make it better for you, but this gets an even number between "cases" out of the max number to make the random number change.
I don't have maya open so I can't test this for you, I'm running a rather large batch so can't do much heh.
Hey trancor,
Thanks for your reply... helpful as always :)
What you explained is a bit more complex than what I really need... I'll try to be more clear:
I have an array with two values, eg ["blue", "red"]
I'll do a number of iterations, let's say 20 iterations
On each of the iterations, I have to choose one of these values (either "blue" or "red")
If I do a simple random choice on each iteration, I'll get more or less each item being chosen an similar number of times (probability is 5050%)
But what I want is to do a weighted randomness, which means that I want to change the probability of each element to be chosen. In this case, I can say "I want random values, but 'blue' has a bigger probability of being chosen than 'red'"
So, I want to get away from the 5050% probability of the simple random, to a 6040% probability of the weighted randomnes....
Got it?
I should be fairly simple, but I can't seem to find any clue....
Robken
07102007, 07:39 AM
maybe you could just make an array of 10 values, 6 blue, and 4 red....
then let it pick between those ;)
see, I told you guys it was simple! ;)
I feel dumb now...
thank you for the idea Robken...
trancor
07112007, 04:02 AM
hurray for misunderstanding, well at least I wrote a neat script haha, I'm going to use it now. hurray for randomized motion, I'm going to pick random percentages and get some neat effects going now.
CGTalk Moderation
07112007, 04:02 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©20002014, Jelsoft Enterprises Ltd.