Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writing a paper on an application I wrote, and I'm trying to describe mathematically what I did to determine the rotation of an object. What I did is really simple, but my math lags far behind my computer science.

What it does is select a random number between min and max. 0 <= min, max < 1 If min > max, in which case it selects a random number between min and max and subtracts 1 if the number is greater than 1. (i.e. the number under (mod 1))

This is what I'm saying right now:

When determining an orientation, the orientation is selected from the valid orientations, then a random number x is selected such that

enter image description here

Where min is the minimum offset, max is the maximum offset, and 0≤min,max<1. The radian measure of the orientation would be 2πx, and the degree measure would be 360x°.

This is the code in question:

if (min > max)
    double d = Utility.r.NextDouble() * (max + 1 - min) + min;
    if (d >= 1)
        d -= 1;
    return d;
    double d = Utility.r.NextDouble() * (max - min) + min;
    return d;
share|cite|improve this question
If you are just trying to say that the function generates a random number in the interval $[0,1)$, then that is all you really need to say. However, if you really do need to describe the exact way you're implementing this then you might want to try optimizing/refactoring your code to attain a more elegant solution. The math would then follow more readily. – poirot Aug 14 '12 at 16:48
@pbs Just thinking about math forced me to realize this could easily be done in a single line. I still have a very similar issue with expressing selecting a random number from a set, so I'll just follow Greg Martin's advice. – Ryan Amos Aug 14 '12 at 17:29
up vote 3 down vote accepted

I think this is much easier to state in words than in mathematical notation. "We are given two real numbers min and max between 0 and 1. The function we coded selects a random real number between 0 and 1 as follows: if min < max, then we select a real number uniformly from the interval [min, max); if min > max, we select a real number uniformly from the interval [max, min+1), and then reduce the resulting number modulo 1."

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.