// The following codes are from
// https://docs.microsoft.com/en-us/quantum/tutorials/quantum-random-number-generator

open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Measurement;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Intrinsic;

// test documentation
operation SampleQuantumRandomNumberGenerator() : Result {
    use q = Qubit();   // Allocate a qubit.
    H(q);              // Put the qubit to superposition. It now has a 50% chance of being 0 or 1.
    return M(q); // Measure the qubit value.
}

operation SampleRandomNumberInRange(max : Int) : Int {
    mutable output = 0;
    repeat {
        mutable bits = [];
        for idxBit in 1..BitSizeI(max) {
            set bits += [SampleQuantumRandomNumberGenerator()];
        }
        set output = ResultArrayAsInt(bits);
    } until (output <= max);
    return output;    
}

%simulate SampleRandomNumberInRange max=50

H?

%lsmagic

