static void Main(string[] args) { using (var qsim = new QuantumSimulator()) { // First we initialize all the variables: var bitString = "0"; // To save the bit string int max = 50; // The maximum of the range int size = Convert.ToInt32(Math.Floor(Math.Log(max, 2.0) + 1)); // To calculate the amount of needed bits int output = max + 1; // Int to store the output while (output > max) // Loop to generate the number { bitString = "0"; // Restart the bit string if fails bitString = String.Join("", Enumerable.Range(0, size).Select(idx => QuantumRandomNumberGenerator.Run(qsim).Result == Result.One ? "1" : "0" ) ); // Generate and concatenate the bits using using the Q# operation output = Convert.ToInt32(bitString, 2); // Convert the bit string to an integer } // Print the result Console.WriteLine($"The random number generated is {output}."); } }
static async Task <bool> GetRandomBool() { using var qsim = new QuantumSimulator(); var result = await QuantumRandomNumberGenerator.Run(qsim); return(result == Result.One); }
public static Task <string> QuantumRandom(int count, int minimum, int maximum) { return(Task.Run(() => { var buffer = new StringBuilder(); var r = new QuantumRandomNumberGenerator(); for (int i = 0; i < count; i++) { buffer.Append(r.Next(minimum, maximum)); buffer.Append("\r\n"); } return buffer.ToString(); })); }
static void Main(string[] args) { // Ping simulator using var quantumSimulator = new QuantumSimulator( ThrowsOnReleasingQuitsNotInZerosState, (uint)Guid.NewGuid().GetHashCode(), DisableBorrowing); PingQ.Run(quantumSimulator).Wait(); // Gen random numbers Console.WriteLine($"{ResultCount} Q#-generated 64-bit random integers:"); for (var d = 0; d < ResultCount; d++) { // Call q# processing here var res = QuantumRandomNumberGenerator.Run(quantumSimulator).Result; Console.WriteLine("{0,20:X16}", res); } }