Contents

BNNSRandomFillUniformFloat(_:_:_:_:)

Fills the specified tensor with random floating-point values from the continuous uniform distribution within a range.

Declaration

func BNNSRandomFillUniformFloat(_ generator: BNNSRandomGenerator?, _ desc: UnsafeMutablePointer<BNNSNDArrayDescriptor>, _ a: Float, _ b: Float) -> 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 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 the descriptor’s data_type isn’t 32-bit floating point, the values that you specify for the bounds must be exactly representable in the descriptor’s data type.

The following code populates a descriptor with random floating-point values:

let data = UnsafeMutableBufferPointer<Float>.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.float,
                                       table_data: nil,
                                       table_data_type: BNNSDataType.float,
                                       data_scale: 1, data_bias: 0)

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

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

print(Array(data))

data.deallocate()
BNNSDestroyRandomGenerator(randomNumberGenerator)

See Also

Random number generation