1.\" Copyright (c) 1990, 1991, 1993 2.\" The Regents of the University of California. 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.\" @(#)rand.3 8.1 (Berkeley) 6/4/93 33.\" 34.Dd February 1, 2020 35.Dt RAND 3 36.Os 37.Sh NAME 38.Nm rand , 39.Nm srand , 40.Nm rand_r 41.Nd bad random number generator 42.Sh LIBRARY 43.Lb libc 44.Sh SYNOPSIS 45.In stdlib.h 46.Ft void 47.Fn srand "unsigned seed" 48.Ft int 49.Fn rand void 50.Ft int 51.Fn rand_r "unsigned *ctx" 52.Sh DESCRIPTION 53.Bf -symbolic 54The functions described in this manual page are not cryptographically 55secure. 56Applications which require unpredictable random numbers should use 57.Xr arc4random 3 58instead. 59.Ef 60.Pp 61The 62.Fn rand 63function computes a sequence of pseudo-random integers in the range 64of 0 to 65.Dv RAND_MAX , 66inclusive. 67.Pp 68The 69.Fn srand 70function seeds the algorithm with the 71.Fa seed 72parameter. 73Repeatable sequences of 74.Fn rand 75output may be obtained by calling 76.Fn srand 77with the same 78.Fa seed . 79.Fn rand 80is implicitly initialized as if 81.Fn srand "1" 82had been invoked explicitly. 83.Pp 84In 85.Fx 13 , 86.Fn rand 87is implemented using the same 128-byte state LFSR generator algorithm as 88.Xr random 3 . 89However, the legacy 90.Fn rand_r 91function is not (and can not be, because of its limited 92.Fa *ctx 93size). 94.Fn rand_r 95implements the historical, poor-quality Park-Miller 32-bit LCG and should not 96be used in new designs. 97.Sh IMPLEMENTATION NOTES 98Since 99.Fx 13 , 100.Fn rand 101is implemented with the same generator as 102.Xr random 3 , 103so the low-order bits should no longer be significantly worse than the 104high-order bits. 105.Sh SEE ALSO 106.Xr arc4random 3 , 107.Xr random 3 , 108.Xr random 4 109.Sh STANDARDS 110The 111.Fn rand 112and 113.Fn srand 114functions 115conform to 116.St -isoC . 117.Pp 118The 119.Fn rand_r 120function is not part of 121.St -isoC 122and is marked obsolescent in 123.St -p1003.1-2008 . 124It may be removed in a future revision of POSIX. 125.Sh CAVEATS 126Prior to 127.Fx 13 , 128.Fn rand 129used the historical Park-Miller generator with 32 bits of state and produced 130poor quality output, especially in the lower bits. 131.Fn rand 132in earlier versions of 133.Fx , 134as well as other standards-conforming implementations, may continue to produce 135poor quality output. 136.Pp 137.Em These functions should not be used in portable applications that want a 138.Em high quality or high performance pseudorandom number generator . 139One possible replacement, 140.Xr random 3 , 141is portable to Linux — but it is not especially fast, nor standardized. 142.Pp 143If broader portability or better performance is desired, any of the widely 144available and permissively licensed SFC64/32, JSF64/32, PCG64/32, or SplitMix64 145algorithm implementations may be embedded in your application. 146These algorithms have the benefit of requiring less space than 147.Xr random 3 148and being quite fast (in header inline implementations). 149