Hello. I've tried to embrace Microsoft DevLabs Code Contracts static analyzer and faced situation when I do not actually know is it me or is it them. So here is the code:
public static int GenerateInBetween(int min, int max)
{
Contract.Requires(min < max);
Contract.Requires((long)(max - min) <= (long)(Int32.MaxValue));
Contract.Ensures(Contract.Result<int>() >= min);
Contract.Ensures(Contract.Result<int>() <= max); // Unpvoven!
long range = max - min;
double basicRandom = new Random().NextDouble();
Contract.Assert(basicRandom >= 0.0);
Contract.Assert(basicRandom <= 1.0); // Unpvoven!
double randomDouble = basicRandom * range;
Contract.Assert(randomDouble >= 0.0);
Contract.Assert(randomDouble <= (double)range); // Unpvoven!
int randomInt32 = (int)randomDouble;
Contract.Assert(randomInt32 >= 0);
Contract.Assert(randomInt32 <= range);
return min + randomInt32;
}
Static analyzer insists that commented post-condition and assert could not be proven. I could not see when it could be wrong.
Edit Even if I replace asserts by assumes post-condition is still unproven.