|
Author |
Topic: the best/easier way to "bias" a random n? (Read 2314 times) |
|
lunetta
|
the best/easier way to "bias" a random n?
« on: Apr 2nd, 2005, 7:36am » |
|
hello all I want to "bias" the result of a random number request; here's the example of a "fair" random run: Quote: // random run int a; int fill0, fill1; int p0, p1; void setup() { size (500,500); fill0 = 0; fill1 = 0; } void loop() { framerate(200); background(255); if (p0 == height) { p0 = 0; fill0+=25; p1 = 0; } if (p1 == height) { p1 = 0; fill1+=25; p0 = 0; } a = int(random(0,2)); println(a); if (a==0) { p0++; } else { p1++; } fill(fill0); rect(100, height-p0, 100, p0); fill (fill1); rect(300, height-p1, 100, p1); } |
| I'n wondering the best way to request a random between 0 and 1, but put a 30% higher probability of having a 0... what's the best way to achieve this? thanks for any response!
|
|
|
|
rgovostes
|
Re: the best/easier way to "bias" a random n?
« Reply #1 on: Apr 3rd, 2005, 10:20pm » |
|
Not positive, but after a few seconds of thought it seems like doing a random between 0 and 1.3 and the subtracting 0.3 from the result and dropping negative values should work - max(0, random(0, 1.3) - 0.3) Update - oh, you just want 0 or 1, not a number between them. So, 65% to get a 0, 35% to get a 1... 13:7 n = (int)Math.floor(random(1, 20)) <= 13 ? 0 : 1; I think...
|
« Last Edit: Apr 3rd, 2005, 10:24pm by rgovostes » |
|
|
|
|
lunetta
|
Re: the best/easier way to "bias" a random n?
« Reply #2 on: Apr 4th, 2005, 6:21am » |
|
nice, thanks. I implemented something very similar, but with more lines of code... I need to learn to use "?" and things like that...
|
|
|
|
kurol
|
Re: the best/easier way to "bias" a random n?
« Reply #3 on: Apr 4th, 2005, 11:53am » |
|
I need to learn tricks like that inline if too. My programming ability is based on Pascal and one C++ course (I never attended a single lecture of that class, not even the 1st day. Still managed to get an A though!). That and some random languages I picked up just because I had to.
|
I hate programming . . .
|
|
|
TomC
|
Re: the best/easier way to "bias" a random n?
« Reply #4 on: Apr 4th, 2005, 1:47pm » |
|
The casting and flooring is a bit extravagant, I think. How about: Code: int n = random(1.0) < 0.65 ? 1 : 0; |
| Which is the same as: Code: int n; if (random(1.0) < 0.65) { n = 1; } else { n = 0; } |
| The ?: operator is handy, but not always clear.
|
« Last Edit: Apr 4th, 2005, 1:48pm by TomC » |
|
|
|
|
mflux
|
Re: the best/easier way to "bias" a random n?
« Reply #5 on: Apr 4th, 2005, 2:24pm » |
|
Kurol! I love your sig! And thanks guys! These are very helpful~~ I have wondered the same thing lunetta had.
|
|
|
|
TomC
|
Re: the best/easier way to "bias" a random n?
« Reply #6 on: Apr 4th, 2005, 4:34pm » |
|
I should add that there are other fun ways to "bias" a number... or should that be "fun" ways to bias a number? If you have a number between 0.0 and 1.0, try squaring it or cubing it (etc) - it stays between 0.0 and 1.0 but gives much lower results on average. Or raise it to a power less than 1.0 to bias it in the higher range. I'm sure you can think of something more inventive, or more efficient, but that might point you in an interesting direction.
|
« Last Edit: Apr 4th, 2005, 4:38pm by TomC » |
|
|
|
|
lunetta
|
Re: the best/easier way to "bias" a random n?
« Reply #7 on: Apr 5th, 2005, 6:31am » |
|
Hey Tom I don't want to sound dumb or anything, but can you give a small example on how to use squaring for randoms in the way you described? I don't know if I got it right... thanks!
|
|
|
|
TomC
|
Re: the best/easier way to "bias" a random n?
« Reply #8 on: Apr 5th, 2005, 12:17pm » |
|
Squaring or cubing (etc) a number between 0.0 and 1.0 makes it smaller, but biased to the lower end of the range (closer to 0.0). Try this code to see how the distribution changes: Code: void setup() { size(300,300); } void draw() { background(0); for (int i = 0; i < 10000; i++) { float a = random(1.0); // a is between 0.0 and 1.0 float b = a*a; // b is between 0.0 and 1.0, but smaller than a float c = a*a*a; // c is between 0.0 and 1.0, but smaller than c float d = pow(a,0.5); // d is between 0.0 and 1.0, but bigger than a // pow(a,0.5) is the same as sqrt(a) stroke(255,4); line(a*width,0,a*width,height/4); line(b*width,height/4,b*width,height/2); line(c*width,height/2,c*width,3*height/4); line(d*width,3*height/4,d*width,height); } } |
| The top quarter of the screen should be evenly distributed (it's still random, so it won't be perfect). The second quarter should be biased towards the lower end, the third quarter should be even more biased towards the lower end, and the fourth should be biased to the higher end. Also, try removing the for loop and setting a to vary with mouseX: Code: void setup() { size(300,300); } void loop() { background(0); float a = float(mouseX)/width; // a is between 0.0 and 1.0 float b = a*a; // b is between 0.0 and 1.0, but smaller than a float c = a*a*a; // c is between 0.0 and 1.0, but smaller than c float d = pow(a,0.5); // d is between 0.0 and 1.0, but bigger than a // pow(a,0.5) is the same as sqrt(a) stroke(255); line(a*width,0,a*width,height/4); line(b*width,height/4,b*width,height/2); line(c*width,height/2,c*width,3*height/4); line(d*width,3*height/4,d*width,height); } |
| Look at how the other lines vary. What happens when the mouse is in the middle, and a is 0.5? One tenth of the way across, a is 0.1, and so on... This is basically the same ideas Reas covers in these examples: http://processing.org/learning/examples/nonlinear.html http://processing.org/learning/examples/simple_curves.html Hope that helps.
|
« Last Edit: Apr 5th, 2005, 2:06pm by TomC » |
|
|
|
|
kurol
|
Re: the best/easier way to "bias" a random n?
« Reply #9 on: Apr 5th, 2005, 8:52pm » |
|
That's a more similar concept to my initial solution to this problem. And that's to basically create a polynomial that describes a probability distribution function.
|
I hate programming . . .
|
|
|
lunetta
|
Re: the best/easier way to "bias" a random n?
« Reply #10 on: Apr 6th, 2005, 7:56pm » |
|
wow thanks for the code Tom, I'm going to study them...
|
|
|
|
|