Contents

BNNSRandomFillUniformInt(_:_:_:_:)

Fills the specified tensor with random integer values from the continuous uniform distribution within a range.

Declaration

func BNNSRandomFillUniformInt(_ generator: BNNSRandomGenerator?, _ desc: UnsafeMutablePointer<BNNSNDArrayDescriptor>, _ a: Int64, _ b: Int64) -> Int32

Parameters

  • generator:

    The random number generator.

  • desc:

    The descriptor of the destination.

  • a:

    The lower bound of distribution.

  • b:

    The upper bound of distribution.

Discussion

Use this function to fill the an array descriptor with uniformly distributed random values.

If you use the same generator on multiple threads, note that this function serializes the generator through an internal lock. To eliminate this contention, use different generators for each thread.

If either bound is outside the range of the representable values for the output descriptor’s data type, the function clips the value to the closest representable value.

The following code populates a descriptor with random integer values:

let data = UnsafeMutableBufferPointer<Int16>.allocate(capacity: 8)
var descriptor = BNNSNDArrayDescriptor(flags: BNNSNDArrayFlags(0),
                                       layout: BNNSDataLayoutVector,
                                       size: (10, 0, 0, 0, 0, 0, 0, 0),
                                       stride: (0, 0, 0, 0, 0, 0, 0, 0),
                                       data: data.baseAddress!,
                                       data_type: BNNSDataType.int16,
                                       table_data: nil,
                                       table_data_type: BNNSDataType.int16,
                                       data_scale: 1, data_bias: 0)

guard let randomNumberGenerator = BNNSCreateRandomGenerator(BNNSRandomGeneratorMethodAES_CTR,
                                                            nil) else {
    return
}

BNNSRandomFillUniformInt(randomNumberGenerator,
                         &descriptor,
                         -10,
                         10)

print(Array(data))

data.deallocate()
BNNSDestroyRandomGenerator(randomNumberGenerator)

See Also

Random number generation