allocate(randomUniformUsing:range:shape:batchSize:)
Returns a new array descriptor that’s initialized with random integer values from the continuous uniform distribution.
Declaration
static func allocate<Scalar>(randomUniformUsing: BNNS.RandomGenerator, range: ClosedRange<Scalar>, shape: BNNS.Shape, batchSize: Int = 1) -> BNNSNDArrayDescriptor? where Scalar : BNNSScalar, Scalar : BinaryFloatingPointParameters
- randomUniformUsing:
The random number generator that provides random values.
- range:
The range of random values.
- shape:
The shape of the n-dimensional array descriptor.
- batchSize:
The number of batches of data.
Discussion
Use this function to create a new array descriptor that’s initialized with random values BNNS.RandomGenerator generates.
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.
The following code creates a 16-element 1D tensor that contains random 16-bit integer values between -10 and 10:
guard
let randomGenerator = BNNS.RandomGenerator(
method: .aesCtr,
seed: 1234),
let descriptor = BNNSNDArrayDescriptor.allocate(
randomUniformUsing: randomGenerator,
range: Int16(-10)...Int16(10),
shape: [16]) else {
return
}
// Prints 16 random values.
print(descriptor.makeArray(of: Int16.self)!)
descriptor.deallocate()See Also
Allocating and Deallocating Memory
allocate(initializingFrom:shape:batchSize:)allocate(randomUniformUsing:range:shape:batchSize:)allocate(randomIn:shape:batchSize:)allocate(randomIn:shape:batchSize:)allocate(randomIn:using:shape:batchSize:)allocate(randomIn:using:shape:batchSize:)allocate(repeating:shape:batchSize:)allocateUninitialized(scalarType:shape:batchSize:)deallocate()