# Creating an array of random numbers in Swift is slow

**URL:** <https://forums.swift.org/t/creating-an-array-of-random-numbers-in-swift-is-slow/80022>\
**Category:** Using Swift\
**Tags:** array, accelerate\
**Created:** [May 21, 2025, 1:04am UTC](https://forums.swift.org/t/creating-an-array-of-random-numbers-in-swift-is-slow/80022 "2025-05-21T01:04:21Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![dnadoba](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dnadoba/32/11438_2.png) [@dnadoba](https://forums.swift.org/u/dnadoba)\
**Post date:** [May 21, 2025, 1:14am UTC](https://forums.swift.org/t/creating-an-array-of-random-numbers-in-swift-is-slow/80022/2 "2025-05-21T01:14:39Z")

</div>

There are two pitches that would address this:

> [@Pitch: Requesting larger amounts of randomness from SystemRandomNumberGenerator](https://forums.swift.org/t/pitch-requesting-larger-amounts-of-randomness-from-systemrandomnumbergenerator/27226):
>
> Introduction I propose adding a new method to SystemRandomNumberGenerator: randomBytes(count:), which will enable generating an arbitrary number of random bytes. This is a minor quality of life pitch that can provide a small but meaningful performance improvement to randomness-heavy code. Motivation [SE-0202](https://github.com/apple/swift-evolution/blob/master/proposals/0202-random-unification.md) introduced the RandomNumberGenerator protocol and provided a single standard library conforming type: SystemRandomNumberGenerator. This SystemRandomNumberGenerator implementation was delib…

> [@An API for bulk random bytes](https://forums.swift.org/t/an-api-for-bulk-random-bytes/63051):
>
> I'm [proposing](https://github.com/stephentyrone/swift-evolution/blob/nose-full-of-random/proposals/nnnn-random-fill.md) that we add the following requirement to the RandomNumberGenerator protocol: mutating func fill(\_ buffer: UnsafeMutableRawBufferPointer) with a default implementation that uses the existing next() API to produce 64b at a time, and a customized implementation for SystemRandomNumberGenerator that directly requests the specified amount of random bytes from the underlying platform APIs. On its own, this doesn't change much, but we can then adopt it within the standard library in cas…

The later also has an implementation that you could use with some tweaks:  
[https://github.com/swiftlang/swift/pull/63511/files](https://github.com/swiftlang/swift/pull/63511/files)

---

_[View the full topic](https://forums.swift.org/t/creating-an-array-of-random-numbers-in-swift-is-slow/80022)._
