microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
billt/revert-mimalloc

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

katas/content/random_numbers/random_number/Solution.qs

27lines · modecode

1namespace Kata {
2 open Microsoft.Quantum.Math;
3 open Microsoft.Quantum.Measurement;
4
5 operation RandomNumberInRange(min : Int, max : Int) : Int {
6 let nBits = BitSizeI(max - min);
7 mutable output = 0;
8 repeat {
9 set output = RandomNBits(nBits);
10 } until output <= max - min;
11 return output + min;
12 }
13
14 operation RandomNBits(N : Int) : Int {
15 mutable result = 0;
16 for i in 0 .. N - 1 {
17 set result = result * 2 + RandomBit();
18 }
19 return result;
20 }
21
22 operation RandomBit() : Int {
23 use q = Qubit();
24 H(q);
25 return MResetZ(q) == Zero ? 0 | 1;
26 }
27}
28