1.\" Copyright (c) 1990, 1991 The Regents of the University of California. 2.\" All rights reserved. 3.\" 4.\" This code is derived from software contributed to Berkeley by 5.\" the American National Standards Committee X3, on Information 6.\" Processing Systems. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 3. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" $OpenBSD: rand.3,v 1.20 2021/02/12 17:03:51 deraadt Exp $ 33.\" 34.Dd $Mdocdate: February 12 2021 $ 35.Dt RAND 3 36.Os 37.Sh NAME 38.Nm rand , 39.Nm rand_r , 40.Nm srand , 41.Nm srand_deterministic 42.Nd bad pseudo-random number generator 43.Sh SYNOPSIS 44.In stdlib.h 45.Ft void 46.Fn srand "unsigned int seed" 47.Ft void 48.Fn srand_deterministic "unsigned int seed" 49.Ft int 50.Fn rand void 51.Ft int 52.Fn rand_r "unsigned int *seed" 53.Sh DESCRIPTION 54.Bf -symbolic 55Standards insist that this interface return deterministic results. 56Unsafe usage is very common, so 57.Ox 58changed the subsystem to return non-deterministic results by default. 59.Ef 60.Pp 61To satisfy portable code, 62.Fn srand 63may be called to initialize the subsystem. 64In 65.Ox 66the 67.Ar seed 68variable is ignored, and strong random number results will be provided from 69.Xr arc4random 3 . 70In other systems, the 71.Ar seed 72variable primes a simplistic deterministic algorithm. 73.Pp 74If the standardized behavior is required 75.Fn srand_deterministic 76can be substituted for 77.Fn srand , 78then subsequent 79.Fn rand 80calls will return results using the deterministic algorithm. 81The deterministic sequence algorithm changed a number of times since 82original development, is underspecified, and should not be relied upon to 83remain consistent between platforms and over time. 84.Pp 85The 86.Fn rand 87function returns a result in the range of 0 to 88.Dv RAND_MAX . 89By default, this result comes from 90.Xr arc4random 3 . 91If 92.Fn srand_deterministic 93was called, the result will be computed using the deterministic algorithm. 94.Pp 95The 96.Fn rand_r 97is a thread-safe version of 98.Fn rand . 99Storage for the seed must be provided through the 100.Fa seed 101argument, and needs to have been initialized by the caller. 102It always operates using the deterministic algorithm. 103.Sh SEE ALSO 104.Xr arc4random 3 , 105.Xr rand48 3 , 106.Xr random 3 107.Sh STANDARDS 108The 109.Fn rand 110function conforms to 111.St -ansiC . 112.Pp 113The 114.Fn rand_r 115function conforms to 116.St -p1003.1-2008 . 117.Pp 118The 119.Fn srand 120function does not conform to 121.St -ansiC , 122intentionally. 123.Pp 124The 125.Fn srand_deterministic 126function is an 127.Ox 128extension. 129.Sh HISTORY 130The functions 131.Fn rand 132and 133.Fn srand 134first appeared in 135.At v3 . 136