diff --git a/src/Qir/Tests/QIR-static/qir-test-math.cpp b/src/Qir/Tests/QIR-static/qir-test-math.cpp index b544ce5c6a1..545d869c436 100644 --- a/src/Qir/Tests/QIR-static/qir-test-math.cpp +++ b/src/Qir/Tests/QIR-static/qir-test-math.cpp @@ -116,6 +116,12 @@ TEST_CASE("QIR: Math.DrawRandomInt", "[qir.math][qir.Math.DrawRandomInt]") REQUIRE(0 == strcmp(exc.what(), Quantum::Qis::Internal::excStrDrawRandomVal)); } + // Test equal minimum and maximum: + for(uint64_t num: { -5, 0, 3 } ) + { + REQUIRE(Microsoft__Quantum__Testing__QIR__Math__TestDrawRandomInt__body(num, num) == num); + } + // There is a strong difference in the opinions about how the random number generator must be tested. // More or less agreed-upon items are: // * The test must be 100% deterministic, i.e. must not fail, even with a very low probability. diff --git a/src/Simulation/QSharpFoundation/Random/Intrinsic.qs b/src/Simulation/QSharpFoundation/Random/Intrinsic.qs index df6dc83534e..2232e824088 100644 --- a/src/Simulation/QSharpFoundation/Random/Intrinsic.qs +++ b/src/Simulation/QSharpFoundation/Random/Intrinsic.qs @@ -17,7 +17,7 @@ namespace Microsoft.Quantum.Random { /// probability. /// /// # Remarks - /// Fails if `max <= min`. + /// Fails if `max < min`. /// /// # Example /// The following Q# snippet randomly rolls a six-sided die: @@ -45,7 +45,7 @@ namespace Microsoft.Quantum.Random { /// uniform probability. /// /// # Remarks - /// Fails if `max <= min`. + /// Fails if `max < min`. /// /// # Example /// The following Q# snippet randomly draws an angle between $0$ and $2 \pi$: diff --git a/src/Simulation/QSharpFoundation/Random/Uniform.qs b/src/Simulation/QSharpFoundation/Random/Uniform.qs index 34c65b39e5e..4e818c4a91f 100644 --- a/src/Simulation/QSharpFoundation/Random/Uniform.qs +++ b/src/Simulation/QSharpFoundation/Random/Uniform.qs @@ -20,7 +20,7 @@ namespace Microsoft.Quantum.Random { /// interval from `min` to `max` with uniform probability. /// /// # Remarks - /// Fails if `max <= min`. + /// Fails if `max < min`. /// /// # Example /// The following Q# snippet randomly draws an angle between $0$ and $2 \pi$: @@ -34,7 +34,7 @@ namespace Microsoft.Quantum.Random { function ContinuousUniformDistribution( min : Double, max : Double ) : ContinuousDistribution { - Fact(max > min, $"Max must be larger than min, but {max} <= {min}."); + Fact(max >= min, $"Max must be greater than or equal to min, but {max} < {min}."); return ContinuousDistribution(Delay(DrawRandomDouble, (min, max), _)); } @@ -52,7 +52,7 @@ namespace Microsoft.Quantum.Random { /// range from `min` to `max` with uniform probability. /// /// # Remarks - /// Fails if `max <= min`. + /// Fails if `max < min`. /// /// # Example /// The following Q# snippet randomly rolls a six-sided die: @@ -64,7 +64,7 @@ namespace Microsoft.Quantum.Random { /// # See Also /// - Microsoft.Quantum.DrawRandomDouble function DiscreteUniformDistribution(min : Int, max : Int) : DiscreteDistribution { - Fact(max > min, $"Max must be larger than min, but {max} <= {min}."); + Fact(max >= min, $"Max must be greater than or equal to min, but {max} < {min}."); return DiscreteDistribution(Delay(DrawRandomInt, (min, max), _)); } diff --git a/src/Simulation/Simulators.Tests/TestProjects/IntrinsicTests/Random/Tests.qs b/src/Simulation/Simulators.Tests/TestProjects/IntrinsicTests/Random/Tests.qs index aff82adbc8a..c646960b606 100644 --- a/src/Simulation/Simulators.Tests/TestProjects/IntrinsicTests/Random/Tests.qs +++ b/src/Simulation/Simulators.Tests/TestProjects/IntrinsicTests/Random/Tests.qs @@ -74,10 +74,25 @@ namespace Microsoft.Quantum.Tests { @Test("QuantumSimulator") @Test("ToffoliSimulator") operation CheckDrawRandomIntObeysRanges() : Unit { - let randomInt = DrawRandomInt(0, 45); + mutable randomInt = DrawRandomInt(0, 45); if (randomInt > 45 or randomInt < 0) { fail $"DrawRandomInt(0, 45) returned {randomInt}, outside the allowed range."; } + + set randomInt = DrawRandomInt(0, 0); + if (randomInt > 0 or randomInt < 0) { + fail $"DrawRandomInt(0, 0) returned {randomInt}, outside the allowed range."; + } + + set randomInt = DrawRandomInt(-3, -3); + if (randomInt > -3 or randomInt < -3) { + fail $"DrawRandomInt(-3, -3) returned {randomInt}, outside the allowed range."; + } + + set randomInt = DrawRandomInt(3, 3); + if (randomInt > 3 or randomInt < 3) { + fail $"DrawRandomInt(3, 3) returned {randomInt}, outside the allowed range."; + } } /// # Summary diff --git a/src/Simulation/Simulators/QuantumSimulator/SimulatorBase.cs b/src/Simulation/Simulators/QuantumSimulator/SimulatorBase.cs index c84cb17936f..8ef72b66aa0 100644 --- a/src/Simulation/Simulators/QuantumSimulator/SimulatorBase.cs +++ b/src/Simulation/Simulators/QuantumSimulator/SimulatorBase.cs @@ -469,16 +469,16 @@ public DrawRandomInt(SimulatorBase m) : base(m) => public override Func<(long, long), long> __Body__ => arg => { var (min, max) = arg; - if (max <= min) + if (max < min) { - throw new ExecutionFailException($"Max must be greater than min, but {max} <= {min}."); + throw new ExecutionFailException($"Max must be greater than or equal to min, but {max} < {min}."); } return sim.RandomGenerator.NextLong(min, max); }; } /// - /// Implements the DrawRandomInt operation from the + /// Implements the DrawRandomDouble operation from the /// Microsoft.Quantum.Random namespace. /// public class DrawRandomDouble : Random.DrawRandomDouble @@ -490,9 +490,9 @@ public DrawRandomDouble(SimulatorBase m) : base(m) => public override Func<(double, double), double> __Body__ => arg => { var (min, max) = arg; - if (max <= min) + if (max < min) { - throw new ExecutionFailException($"Max must be greater than min, but {max} <= {min}."); + throw new ExecutionFailException($"Max must be greater than or equal to min, but {max} < {min}."); } var delta = max - min; return min + delta * sim.RandomGenerator.NextDouble();