Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

BNNS random number generator for Double value types

I generate an array of random floats using the code shown below. However, I would like to do this with Double instead of Float. Are there any BNNS random number generators for double values, something like BNNSRandomFillUniformDouble? If not, is there a way I can convert BNNSNDArrayDescriptor from float to double?

import Accelerate

let n = 100_000_000

let result = Array<Float>(unsafeUninitializedCapacity: n) { buffer, initCount in

    var descriptor = BNNSNDArrayDescriptor(data: buffer, shape: .vector(n))!
    let randomGenerator = BNNSCreateRandomGenerator(BNNSRandomGeneratorMethodAES_CTR, nil)

    BNNSRandomFillUniformFloat(randomGenerator, &descriptor, 0, 1)

    initCount = n
}

Hello,

I'm not aware of a BNNSRandomFillUniformDouble, which is better than trying to workaround Swift type-safety.

What is the use case?

Please consider sending us an enhancement request detailing the use case with the Feedback Assistant.

To confirm, there's no Double support for BNNS random fill.

vDSP has very fast single- to double-precision conversion. The C API function is vDSP_vspdp and the Swift version is vDSP.convertElements

I updated my example with vDSP.convertElements but this requires two arrays which are result and resultDouble. Is there a way to do this with just one array instead of two?

import Accelerate

let n = 100_000_000

let result = Array<Float>(unsafeUninitializedCapacity: n) { buffer, initCount in

    var descriptor = BNNSNDArrayDescriptor(data: buffer, shape: .vector(n))!
    let randomGenerator = BNNSCreateRandomGenerator(BNNSRandomGeneratorMethodAES_CTR, nil)

    BNNSRandomFillUniformFloat(randomGenerator, &descriptor, 0, 1)

    initCount = n
}

var resultDouble = [Double](repeating: 0, count: n)
vDSP.convertElements(of: result, to: &resultDouble)
BNNS random number generator for Double value types
 
 
Q