Unix::OpenBSD::Random - interface to arc4random(3) on OpenBSD


  use Unix::OpenBSD::Random qw(arc4random arc4random_uniform);

  my $x = arc4random();
  my $y = arc4random(2);    # coinflip


This module is a wafer-thin wraper around the arc4random(3) library function on OpenBSD. Other OS have this function call though may require arc4random_stir(3) or such calls that this module does not support.



Returns an integer in the uint32_t range.

arc4random_buf length

Returns a string filled with the given number of bytes of random data. This string may contain non-printable or even NUL characters so might best be converted to some other form before being displayed or used where such characters may cause problems.

  my $buf = arc4random_buf(8);

  printf "%vx\n", $buf;

  my $string = unpack "H*", $buf;

Will throw an exception if the length is outside the range of a size_t.

Note that this interface has been made more Perl-like than the C version arc4random_buf(buf, nbytes).

arc4random_uniform upper_bound

Returns an integer no more than the upper_bound. Avoids modulo bias. Will throw an exception if upper_bound is outside the range of allowed values for uint32_t.


Reporting Bugs

Please report any bugs or feature requests to bug-unix-openbsd-random at, or through the web interface at

Patches might best be applied towards:

Known Issues

The newly being written thing and lack of testing on 32-bit systems. Lack of XS skill on the part of the author.

arc4random_uniform accepts 0 as an upper bound (and arc4random_buf a length of 0). If this is a problem add a check before calling into this module.



thrig - Jeremy Mates (cpan:JMATES) <jmates at>


Copyright (C) 2018 by Jeremy Mates

This program is distributed under the (Revised) BSD License: