Weerel
BAN USERThat angle is a periodic function. Inside its period the function is linear. Thus all we need is find a period. If x is a period and minute hand rotates 12 time faster than the hour hand, then we can write down equation for that:12x-12=x. Thus x=12/11of hour is a period when the minute hand over the hour hand. It's very efficient to store pre calculations in constants. So the function requires only two operations to be computed. One should note that function returns angle in radians.
public class Clock
{
private static long Period = TimeSpan.FromHours(1).Ticks * 12 / 11;
private static double Scale = 2 * Math.PI / Period;
public double GetAngle(TimeSpan time)
{
return Scale * (time.Ticks % Period);
}
}
I would use something like that:
The worst case is when the range is equals to uint.MaxValue/2 + 1. It means that probability of condition in the loop to be true is about 1/2. It means that expected value of Random() calls in Random(r1, r2) equals to 2. In others cases expected value will be close to 1. So number of calls inside Random(r1, r2) can be very large, but there expected value in worst case is 2 and about 1 in most cases, this is not too bad.
- Weerel February 08, 2012