473,320 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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 1720

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
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
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" <mr******@discussions.microsoft.comwrote in message
news:24**********************************@microsof t.com...
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 #4
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

"Ken Fine" <ke*****@newsgroup.nospamwrote in message
news:eU**************@TK2MSFTNGP04.phx.gbl...
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.
Use the arctangent function (atn). 99+% of inputs are compressed very close
to a single point (180 degrees or pi radians). Multiplication by a factor
before feeding into the atn function will affect your "gravity", addition
after running atn will determine your center point.

You might find y = g / (x * (360 - x)) to be useful as well. Use y = the
return value from rand. Then:

g / y = 360 * x - x*x
x*x - 360*x + g / y = 0
(apply quadratic formula)
x = 180 +/- sqrt(180*180 - g / y)

So there ya are:

degrees = 180 +/- sqrt(180*180 - g / (1.0 + rand()))

Pick either plus or minus with equal probability to get angles both sides of
zero, minus only for just positive angles, plus only for just negative
angles (actually very near 360)
g can vary from 0 to 180*180, you get more spread with bigger g.
Change the first 180 to move the center point. The angles you get will be
clustered 180 degrees away from the value you use.
>
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

"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:u3**************@TK2MSFTNGP06.phx.gbl...
>
"Ken Fine" <ke*****@newsgroup.nospamwrote in message
news:eU**************@TK2MSFTNGP04.phx.gbl...
>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.

Use the arctangent function (atn). 99+% of inputs are compressed very
close to a single point (180 degrees or pi radians). Multiplication by a
factor before feeding into the atn function will affect your "gravity",
addition after running atn will determine your center point.

You might find y = g / (x * (360 - x)) to be useful as well. Use y = the
return value from rand. Then:

g / y = 360 * x - x*x
x*x - 360*x + g / y = 0
(apply quadratic formula)
x = 180 +/- sqrt(180*180 - g / y)

So there ya are:

degrees = 180 +/- sqrt(180*180 - g / (1.0 + rand()))

Pick either plus or minus with equal probability to get angles both sides
of zero, minus only for just positive angles, plus only for just negative
angles (actually very near 360)
g can vary from 0 to 180*180, you get more spread with bigger g.
Change the first 180 to move the center point. The angles you get will be
clustered 180 degrees away from the value you use.
I think I messed that up slightly when I added the 1.0 in the denominator to
protect against negative numbers. It would still work, but you're going to
have a lot of clustering no matter what. Try this instead:

degrees = center + interval +/- sqrt(interval*interval*(1.0 - 1.0 / (1.0 +
spreadiness * rand())));

You'll want spreadiness to be small and non-zero, between 0.0001 or so and
maybe .01
center and interval are the angle the results group around and the maximum
spread in the results, respectively.
>>
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 #7
Semi-minor correction:

Peter Duniho wrote:
[...]
unitRand = Math.Sign(unitRand) * Math.Pow(Math.Abs(unitRand), 1
/ gravity);
That should read "Math.Abs(1 - unitRand)" instead of
"Math.Abs(unitRand)". Sorry!
Aug 25 '07 #8
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));
}
In "The Art Of Computer Programming" (Volume 2), Knuth gives a couple of
algorithms for generating numbers normally disributed (on the usual bell-shaped
curve) between 0 and 1. Once you have those I believe it is not too difficult
(see any decent statistics textbook) to transform them into numbers normally
distributed around a given mean and having a given standard distribution. This
sounds close to what you are looking for.

Or, you could look up "Normal Distribution" in Wikipedia - good information
there as well, wrapped in somewhat less intimidating math . . .

HTH,
-rick-
Aug 25 '07 #9
Thanks to peanut, Rossum, Peter, Ben, and Rick for a really interesting
discussion. (Who says USENET is dead?) I appreciate the pointers and input
from everyone!

-KF
"Rick Lones" <Wr******@YcharterZ.netwrote in message
news:Mb**********@newsfe02.lga...
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));
}

In "The Art Of Computer Programming" (Volume 2), Knuth gives a couple of
algorithms for generating numbers normally disributed (on the usual
bell-shaped curve) between 0 and 1. Once you have those I believe it is
not too difficult (see any decent statistics textbook) to transform them
into numbers normally distributed around a given mean and having a given
standard distribution. This sounds close to what you are looking for.

Or, you could look up "Normal Distribution" in Wikipedia - good
information there as well, wrapped in somewhat less intimidating math . .
.

HTH,
-rick-
Aug 26 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: KellyH | last post by:
Hi, I hope someone can point me in the right direction. I'll get it out of the way: Yes, I am a college student. No, I am not looking for anyone to do my homework, just looking for help. I have...
2
by: Oliver Plohmann | last post by:
Hello! In JDK1.5 Sun has introduced autoboxing. The user does not need to convert an instance of Integer into an int herself anymore when she wants to do arithmetic operations with the int. The...
23
by: Thomas Mlynarczyk | last post by:
I remember there is a programming language where you can initialize the random number generator, so that it can - if you want - give you the exactly same sequence of random numbers every time you...
7
by: Mark Healey | last post by:
Do the trig functions in math.h work in degrees, radians or what? For some reason it doesn't say which in "man math.h" IIRC the arctan of a slope gives the angle. So, shouldn't atanf((float)1)...
4
by: CSharpguy | last post by:
I'm not sure if this is the correct forum or not, but I have a basic question. Currently we have are doing calculations via stored procedures and then returning the results back to the client in...
10
by: David Coleman | last post by:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1) In the Command Window I get the following ? Math.Round(0.715, 2) 0.72 ? Math.Round(0.725, 2) 0.72 ? Math.Round(0.735, 2)...
10
by: Ing. Carlos Villaseor M. | last post by:
Hi everybody! I have developed in C# and got in a news group a math class that make matrix operations, eigenvals, eigenvecs, stat functions and much more, but now I trying to develop software in...
15
by: lbrtchx | last post by:
I am trying to write up a page with Math formulas (statistical ones) ~ http://www.geocities.com/tekmonk2005/OnLineStats02.html ~ The thing is that I am not able to make it look OK using HTML. I...
8
by: brad | last post by:
How does one make the math module spit out actual values without using engineer or scientific notation? I get this from <code>print math.pow(2,64)</code>: 1.84467440737e+19 I want this:...
34
by: Johannes Baagoe | last post by:
About Math.random(), ECMA 262 just says "Returns a number value with positive sign, greater than or equal to 0 but less than 1, chosen randomly or pseudo randomly with approximately uniform...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.