Tuesday, 20 August 2013

Trying to get an "adjusted" ratio of two numbers

Trying to get an "adjusted" ratio of two numbers

I would like to do this
I am having two numbers, A, B where it can be A>B or A
Compare B with A
if B < A , then take random numbers and check if their ratio meets the
condition
0.95 < ratio <1.05
if not, try another random.
My problem is that I ran into infinite loops... this is what I do
float ratio = A/B;
if (ratio < 0.95 || ratio > 1.05){
do {
float random_n = ((float)rand())/RAND_MAX * - 0.50; //to take
"randomly a negative or positive number
random_n *=0.1; //to get an even smaller step
A+=random_n; // add or subtract (depending on the sign of the
random number) the random number
ratio = A/B; /form the ratio again
cout<<"lets see "<< A <<" "<< B <<" "<<ratio<<endl;
}
while(ratio >0.95 || ratio<1.05);
}
thanks
Alex

No comments:

Post a Comment