Random operation

Warning

This documentation refers to the Classic QDK, which has been replaced by the Modern QDK.

Please see https://aka.ms/qdk.api for the API documentation for the Modern QDK.

Namespace: Microsoft.Quantum.Intrinsic

Package: Microsoft.Quantum.QSharp.Foundation

Warning

Random has been deprecated. Please use DrawCategorical operation instead.

The random operation takes an array of doubles as input, and returns a randomly-selected index into the array as an Int. The probability of selecting a specific index is proportional to the value of the array element at that index. Array elements that are equal to zero are ignored and their indices are never returned. If any array element is less than zero, or if no array element is greater than zero, then the operation fails.

operation Random (probs : Double[]) : Int

Input

probs : Double[]

An array of floating-point numbers proportional to the probability of selecting each index.

Output : Int

An integer $i$ with probability $\Pr(i) = p_i / \sum_i p_i$, where $p_i$ is the $i$th element of probs.