Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
RAND(3)		       FreeBSD Library Functions Manual		       RAND(3)

NAME
     rand, rand_r, srand, srand_deterministic -- bad pseudo-random number gen-
     erator

SYNOPSIS
     #include <stdlib.h>

     void
     srand(unsigned int	seed);

     void
     srand_deterministic(unsigned int seed);

     int
     rand(void);

     int
     rand_r(unsigned int *seed);

DESCRIPTION
     Standards insist that this	interface return deterministic results.	 Un-
     safe usage	is very	common,	so OpenBSD changed the subsystem to return
     non-deterministic results by default.

     To	satisfy	portable code, srand() may be called to	initialize the subsys-
     tem.  In OpenBSD the seed variable	is ignored, and	strong random number
     results will be provided from arc4random(3).  In other systems, the seed
     variable primes a simplistic deterministic	algorithm.

     If	the standardized behavior is required srand_deterministic() can	be
     substituted for srand(), then subsequent rand() calls will	return results
     using the deterministic algorithm.	 The deterministic sequence algorithm
     changed a number of times since original development, is underspecified,
     and should	not be relied upon to remain consistent	between	platforms and
     over time.

     The rand()	function returns a result in the range of 0 to RAND_MAX.  By
     default, this result comes	from arc4random(3).  If	srand_deterministic()
     was called, the result will be computed using the deterministic algo-
     rithm.

     The rand_r() is a thread-safe version of rand().  Storage for the seed
     must be provided through the seed argument, and needs to have been	ini-
     tialized by the caller.  It always	operates using the deterministic algo-
     rithm.

SEE ALSO
     arc4random(3), rand48(3), random(3)

STANDARDS
     The rand()	function conforms to ANSI X3.159-1989 ("ANSI C89").

     The rand_r() function conforms to IEEE Std	1003.1-2008 ("POSIX.1").

     The srand() function does not conform to ANSI X3.159-1989 ("ANSI C89"),
     intentionally.

     The srand_deterministic() function	is an OpenBSD extension.

HISTORY
     The functions rand() and srand() first appeared in	Version	3 AT&T UNIX.

FreeBSD	13.0		       February	12, 2021		  FreeBSD 13.0

NAME | SYNOPSIS | DESCRIPTION | SEE ALSO | STANDARDS | HISTORY

Want to link to this manual page? Use this URL:
<https://www.freebsd.org/cgi/man.cgi?query=rand&sektion=3&manpath=OpenBSD+6.9>

home | help