446,206 Members | 1,035 Online Need help? Post your question and get tips & solutions from a community of 446,206 IT Pros & Developers. It's quick & easy.

# Basic math Q: how do I make a number-generation function that is biased toward a specified numeric value

 P: n/a Hi there, I have written a simple function that attempts to set the angle of objects so as to place them in aesthetically appealing ways. The code follows; there is some stupidness in it (e.g. a test for meaningless <=180 as a boolean test). The function is designed to return values that will avoid positioning things a) upside down and b) at "too steep" an angle. I would now like to know how I could write my function so that the machine would pick semi-random numbers, but so that those numbers would be biased toward a prespecified value. Ideally it could be parameterized such that you could choose (for lack of a better term) a "gravity" setting. In other words, there would be paramaterized "knob" that indicated how willing you were to let the numbers deviate from your prespecified center value. Low "gravity" would let the numbers float all over the place, with only slight bias. High gravity would bias the selection very strongly toward the center. This would be useful in all sorts of ways. I should have paid more attention in math class. Can someone offer a suggestion? Code follows. private int GetRandomAngle() { Random rand = this.Session["MyRandom"] as Random; int rand1= rand.Next(0, 360); if (rand1 >= 180) { return(rand.Next(270,360)); } if (rand1 <=180) { return(rand.Next(10, 70)); } return (rand.Next(280, 340)); } Aug 24 '07 #1
9 Replies

 P: n/a Start with an initial value Xi. Obtain a random number R between 0 and 1. Subtract Y: Y=0.5 gives a non biased multiplier, <0.5 gives a positive biased multiplier, >0.5 gives a negative bias multiplier. Gravity=(R+Y) New Value = Xi + (R-Y)(Increment) "Ken Fine" wrote: Hi there, I have written a simple function that attempts to set the angle of objects so as to place them in aesthetically appealing ways. The code follows; there is some stupidness in it (e.g. a test for meaningless <=180 as a boolean test). The function is designed to return values that will avoid positioning things a) upside down and b) at "too steep" an angle. I would now like to know how I could write my function so that the machine would pick semi-random numbers, but so that those numbers would be biased toward a prespecified value. Ideally it could be parameterized such that you could choose (for lack of a better term) a "gravity" setting. In other words, there would be paramaterized "knob" that indicated how willing you were to let the numbers deviate from your prespecified center value. Low "gravity" would let the numbers float all over the place, with only slight bias. High gravity would bias the selection very strongly toward the center. This would be useful in all sorts of ways. I should have paid more attention in math class. Can someone offer a suggestion? Code follows. private int GetRandomAngle() { Random rand = this.Session["MyRandom"] as Random; int rand1= rand.Next(0, 360); if (rand1 >= 180) { return(rand.Next(270,360)); } if (rand1 <=180) { return(rand.Next(10, 70)); } return (rand.Next(280, 340)); } Aug 24 '07 #2

 P: n/a sorry, that's gravity=R-Y "mr peanut" wrote: > Start with an initial value Xi. Obtain a random number R between 0 and 1. Subtract Y: Y=0.5 gives a non biased multiplier, <0.5 gives a positive biased multiplier, >0.5 gives a negative bias multiplier. Gravity=(R+Y) New Value = Xi + (R-Y)(Increment) "Ken Fine" wrote: Hi there, I have written a simple function that attempts to set the angle of objects so as to place them in aesthetically appealing ways. The code follows; there is some stupidness in it (e.g. a test for meaningless <=180 as a boolean test). The function is designed to return values that will avoid positioning things a) upside down and b) at "too steep" an angle. I would now like to know how I could write my function so that the machine would pick semi-random numbers, but so that those numbers would be biased toward a prespecified value. Ideally it could be parameterized such that you could choose (for lack of a better term) a "gravity" setting. In other words, there would be paramaterized "knob" that indicated how willing you were to let the numbers deviate from your prespecified center value. Low "gravity" would let the numbers float all over the place, with only slight bias. High gravity would bias the selection very strongly toward the center. This would be useful in all sorts of ways. I should have paid more attention in math class. Can someone offer a suggestion? Code follows. private int GetRandomAngle() { Random rand = this.Session["MyRandom"] as Random; int rand1= rand.Next(0, 360); if (rand1 >= 180) { return(rand.Next(270,360)); } if (rand1 <=180) { return(rand.Next(10, 70)); } return (rand.Next(280, 340)); } Aug 24 '07 #3

 P: n/a Thank you, Mr. Peanut. I have written what I think is a function that follows your math, but I think I'm missing something. What exactly is "increment" in this case? Here is my nonfunctional function: it does something ;) but it doesn't seem to be working in the way it should be: private double GetBiasedAngle(int centerAngle, double biasValue, double increment) { Random rand = this.Session["MyRandom"] as Random; double myrandom = rand.NextDouble(); double biasedAngle = centerAngle + ((myrandom - biasValue)*increment); double finalAngle = Convert.ToInt32(biasedAngle); return finalAngle; } Thanks again for your help and explanation. -KF "mr peanut" >Start with an initial value Xi.Obtain a random number R between 0 and 1. Subtract Y: Y=0.5 gives a nonbiased multiplier, <0.5 gives a positive biased multiplier, >0.5 gives anegative bias multiplier. Gravity=(R+Y)New Value = Xi + (R-Y)(Increment) "Ken Fine" wrote: Hi there, I have written a simple function that attempts to set the angle of objects so as to place them in aesthetically appealing ways. The code follows; there is some stupidness in it (e.g. a test for meaningless <=180 as a boolean test). The function is designed to return values that will avoid positioning things a) upside down and b) at "too steep" an angle. I would now like to know how I could write my function so that the machine would pick semi-random numbers, but so that those numbers would be biased toward a prespecified value. Ideally it could be parameterized such that you could choose (for lack of a better term) a "gravity" setting. In other words, there would be paramaterized "knob" that indicated how willing you were to let the numbers deviate from your prespecified center value. Low "gravity" would let the numbers float all over the place, with only slight bias. High gravity would bias the selection very strongly toward the center. This would be useful in all sorts of ways. I should have paid more attention in math class. Can someone offer a suggestion? Code follows. private int GetRandomAngle() { Random rand = this.Session["MyRandom"] as Random; int rand1= rand.Next(0, 360); if (rand1 >= 180) { return(rand.Next(270,360)); } if (rand1 <=180) { return(rand.Next(10, 70)); } return (rand.Next(280, 340)); } Aug 24 '07 #4

 P: n/a Yes, there are finer models. Many approaches will use the form we have: new value = Xi + modifier (or perhaps new=Xi(factor)) In my linear example modifier = (R-Y)(Increment) Increment is a value you choose that is related to the typical amount you want your Xi value to change each time the function executes. More importantly (I think) to your application is Y. This is a value you can use as a âKnobâ to shift the bias on the new value. Whatever model you choose, you should be able to identify a similar parameter. In my mindâs eye I pictured an application where you use a virtual knob to adjust Y. Aug 25 '07 #5

 P: n/a "Ken Fine" I should have paid more attention in math class. Can someone offer a suggestion? Code follows. private int GetRandomAngle() { Random rand = this.Session["MyRandom"] as Random; int rand1= rand.Next(0, 360); if (rand1 >= 180) { return(rand.Next(270,360)); } if (rand1 <=180) { return(rand.Next(10, 70)); } return (rand.Next(280, 340)); } Aug 25 '07 #6 