# FixedWidthInteger.random()

**URL:** <https://forums.swift.org/t/fixedwidthinteger-random/66145>\
**Category:** Pitches\
**Tags:** random-api, stdlib\
**Created:** [July 13, 2023, 10:02pm UTC](https://forums.swift.org/t/fixedwidthinteger-random/66145 "2023-07-13T22:02:32Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [July 14, 2023, 12:57pm UTC](https://forums.swift.org/t/fixedwidthinteger-random/66145/7 "2023-07-14T12:57:50Z")

</div>

See the following previous thread:

> [@Rename \`FixedWidthInteger.\_random()\` to \`FixedWidthInteger.random(in: UnboundedRange)\`](https://forums.swift.org/t/rename-fixedwidthinteger-random-to-fixedwidthinteger-random-in-unboundedrange/14327/):
>
> On master branch, we have [FixedWidthInteger.\_random()](https://github.com/apple/swift/blob/a6952decab6f918a9df3c6fa342153a9f9204f8e/stdlib/public/core/Integers.swift.gyb#L2939-L2956) function for sampling random numbers from full range. I think we should have FixedWidthInteger.random(in: UnboundedRange) for this purpose. There is FixedWidthInteger.random(in: Range\<Self\>) function. So adding UnboundedRange version makes API more consistent. If someone misses \_random(), he may use FixelWidthInteger.random(in: 0 ... .max). Not only it looks ugly, Range version is a bit slower than \_random(). Having no underscored random l…

* * *

> [@Nevin](#):
>
> If the fixed-width integer type is unsigned, then you can call `rng.next() as T`.
> 
> Is there a principled reason for not allowing this with signed types as well?

The general solution for filling any buffer with random bytes is mentioned in the thread linked above and [has been pitched](https://forums.swift.org/t/an-api-for-bulk-random-bytes/63051); it is to be re-pitched Any Day Now(TM).

> [@anon9791410](#):
>
> The arguments related to modulo bias, which I don't agree with, do not apply to `.all`, which should have come with Swift 4.2.

The spelling for an unbounded range is `...`, so I don't see why it "should have" another spelling. It is absolutely intended that `...` should be supported, and that it is not is an implementation shortcoming [previously discussed in this thread](https://forums.swift.org/t/gold-plating-unboundedrange/12998).

---

_[View the full topic](https://forums.swift.org/t/fixedwidthinteger-random/66145)._
