Skip to content
Commit c622ac1b authored by Adhemerval Zanella's avatar Adhemerval Zanella
Browse files

stdlib: Simplify arc4random_uniform

It uses the bitmask with rejection [1], which calculates a mask
being the lowest power of two bounding the request upper bound,
successively queries new random values, and rejects values
outside the requested range.

Performance-wise, there is no much gain in trying to conserve
bits since arc4random is wrapper on getrandom syscall.  It should
be cheaper to just query a uint32_t value.  The algorithm also
avoids modulo and divide operations, which might be costly
depending of the architecture.

[1] https://www.pcg-random.org/posts/bounded-rands.html



Reviewed-by: default avatarYann Droneaud <ydroneaud@opteya.com>
parent 7187efd0
Loading
Loading
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment