0

So, basically, I did the tutorial to create a random number on the website of Microsoft Azure and now I am trying to add some functionalities, including their suggestion add a minimum number.

The initial code to generate just one number, max, is:

   operation SampleRandomNumberInRange(max : Int) : Int {
        // mutable means variables that can change during computation
        mutable output = 0;
        // repeat loop to generate random numbers until it generates one that is less or equal to max
        repeat {
            mutable bits = new Result[0];
            for idxBit in 1..BitSizeI(max) {
                set bits += [GenerateRandomBit()];
            }
            // ResultArrayAsInt is from Microsoft.Quantum.Convert library, converts string to positive integer
            set output = ResultArrayAsInt(bits);
        } until (output <= max);
        return output;
    }

    @EntryPoint()
    operation SampleRandomNumber() : Int {
        // let declares var which don't change during computation
        let max = 50;
        Message($"Sampling a random number between 0 and {max}: ");
        return SampleRandomNumberInRange(max);
    }

Everything works well. Now, I want to generate two numbers so I would like to create a function TwoSampleRandomNumbersInRange but I can't figure out how to make the function return a result such as "Int, Int", I tried a few things including the follow:

    operation TwoSampleRandomNumbersInRange(min: Int, max : Int) : Int {
        // mutable means variables that can change during computation
        mutable output = 0;
        // repeat loop to generate random numbers until it generates one that is less or equal to max
        repeat {
            mutable bits = new Result[0];
             
            for idxBit in 1..BitSizeI(max) {
                set bits += [GenerateRandomBit()];
            }

            for idxBit in 1..BitSizeI(min) {
                set bits += [GenerateRandomBit()];
            }
            // ResultArrayAsInt is from Microsoft.Quantum.Convert library, converts string to positive integer
            set output = ResultArrayAsInt(bits);
        } until (output >= min and output <= max);
        return output;
    }

To generate two numbers, I tried this:

operation TwoSampleRandomNumbersInRange(min: Int, max : Int) : Int, Int {
//code here
}

...but the syntax for the output isn't right.

I also need the output:

set output = ResultArrayAsInt(bits);

to have two numbers but ResultArrayAsInt, as the name says, just returns an Int. I need to return two integers.

Any help appreciated, thanks!

NiHaoLiHai
  • 23
  • 1
  • 8

2 Answers2

1

The return of an operation has to be a data type, in this case to represent a pair of integers you need a tuple of integers: (Int, Int).

So the signature of your operation and the return statement will be

operation TwoSampleRandomNumbersInRange(min: Int, max : Int) : (Int, Int) {
    // code here
    return (integer1, integer2);
}
Mariia Mykhailova
  • 1,987
  • 1
  • 9
  • 18
  • Thanks, that was helpful for the syntax. I finally found the answer to my own question, I realized today I didn't need to return two integers but just one within the min-max range. – NiHaoLiHai Feb 15 '21 at 17:18
  • @NiHaoLiHai Oh, I see. In the question you asked how to return two numbers instead of one; but what you actually needed to do is to generate a single number between min and max instead of between 0 and max. – Mariia Mykhailova Feb 15 '21 at 20:47
  • 1
    Yeah I got confused so my question actually didn't make much sense. I'll mark your answer as the right one anyway then. Thanks for the help! – NiHaoLiHai Feb 16 '21 at 06:30
0

I found the answer to my own question, all I had to do was:

    operation SampleRandomNumberInRange(min: Int, max : Int) : Int {
        // mutable means variables that can change during computation
        mutable output = 0;
        // repeat loop to generate random numbers until it generates one that is less or equal to max
        repeat {
            mutable bits = new Result[0];
            for idxBit in 1..BitSizeI(max) {
                set bits += [GenerateRandomBit()];
            }
            // ResultArrayAsInt is from Microsoft.Quantum.Convert library, converts string to positive integer
            set output = ResultArrayAsInt(bits);
        } until (output >= min and output <= max);
        return output;
    }

    @EntryPoint()
    operation SampleRandomNumber() : Int {
        // let declares var which don't change during computation
        let max = 50;
        let min = 10;
        Message($"Sampling a random number between {min} and {max}: ");
        return SampleRandomNumberInRange(min, max);
    }

    
}
NiHaoLiHai
  • 23
  • 1
  • 8