|
Packit |
7cfc04 |
.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" %%%LICENSE_START(VERBATIM)
|
|
Packit |
7cfc04 |
.\" Permission is granted to make and distribute verbatim copies of this
|
|
Packit |
7cfc04 |
.\" manual provided the copyright notice and this permission notice are
|
|
Packit |
7cfc04 |
.\" preserved on all copies.
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" Permission is granted to copy and distribute modified versions of this
|
|
Packit |
7cfc04 |
.\" manual under the conditions for verbatim copying, provided that the
|
|
Packit |
7cfc04 |
.\" entire resulting derived work is distributed under the terms of a
|
|
Packit |
7cfc04 |
.\" permission notice identical to this one.
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" Since the Linux kernel and libraries are constantly changing, this
|
|
Packit |
7cfc04 |
.\" manual page may be incorrect or out-of-date. The author(s) assume no
|
|
Packit |
7cfc04 |
.\" responsibility for errors or omissions, or for damages resulting from
|
|
Packit |
7cfc04 |
.\" the use of the information contained herein. The author(s) may not
|
|
Packit |
7cfc04 |
.\" have taken the same level of care in the production of this manual,
|
|
Packit |
7cfc04 |
.\" which is licensed free of charge, as they might when working
|
|
Packit |
7cfc04 |
.\" professionally.
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" Formatted or processed versions of this manual, if unaccompanied by
|
|
Packit |
7cfc04 |
.\" the source, must acknowledge the copyright and authors of this work.
|
|
Packit |
7cfc04 |
.\" %%%LICENSE_END
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" References consulted:
|
|
Packit |
7cfc04 |
.\" Linux libc source code
|
|
Packit |
7cfc04 |
.\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
|
|
Packit |
7cfc04 |
.\" 386BSD man pages
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" Modified 1993-03-29, David Metcalfe
|
|
Packit |
7cfc04 |
.\" Modified 1993-04-28, Lars Wirzenius
|
|
Packit |
7cfc04 |
.\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
|
|
Packit |
7cfc04 |
.\" Modified 1995-05-18, Rik Faith (faith@cs.unc.edu) to add
|
|
Packit |
7cfc04 |
.\" better discussion of problems with rand on other systems.
|
|
Packit |
7cfc04 |
.\" (Thanks to Esa Hyyti{ (ehyytia@snakemail.hut.fi).)
|
|
Packit |
7cfc04 |
.\" Modified 1998-04-10, Nicolás Lichtmaier <nick@debian.org>
|
|
Packit |
7cfc04 |
.\" with contribution from Francesco Potorti <F.Potorti@cnuce.cnr.it>
|
|
Packit |
7cfc04 |
.\" Modified 2003-11-15, aeb, added rand_r
|
|
Packit |
7cfc04 |
.\" 2010-09-13, mtk, added example program
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.TH RAND 3 2017-07-13 "" "Linux Programmer's Manual"
|
|
Packit |
7cfc04 |
.SH NAME
|
|
Packit |
7cfc04 |
rand, rand_r, srand \- pseudo-random number generator
|
|
Packit |
7cfc04 |
.SH SYNOPSIS
|
|
Packit |
7cfc04 |
.nf
|
|
Packit |
7cfc04 |
.B #include <stdlib.h>
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.B int rand(void);
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.BI "int rand_r(unsigned int *" seedp );
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.BI "void srand(unsigned int " seed );
|
|
Packit |
7cfc04 |
.fi
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.in -4n
|
|
Packit |
7cfc04 |
Feature Test Macro Requirements for glibc (see
|
|
Packit |
7cfc04 |
.BR feature_test_macros (7)):
|
|
Packit |
7cfc04 |
.in
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.BR rand_r ():
|
|
Packit |
7cfc04 |
.RS 4
|
|
Packit |
7cfc04 |
Since glibc 2.24:
|
|
Packit |
7cfc04 |
_POSIX_C_SOURCE >= 199506L
|
|
Packit |
7cfc04 |
.br
|
|
Packit |
7cfc04 |
Glibc 2.23 and earlier
|
|
Packit |
7cfc04 |
_POSIX_C_SOURCE
|
|
Packit |
7cfc04 |
.RE
|
|
Packit |
7cfc04 |
.SH DESCRIPTION
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
.BR rand ()
|
|
Packit |
7cfc04 |
function returns a pseudo-random integer in the range 0 to
|
|
Packit |
7cfc04 |
.BR RAND_MAX
|
|
Packit |
7cfc04 |
inclusive (i.e., the mathematical range [0,\ \fBRAND_MAX\fR]).
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
.BR srand ()
|
|
Packit |
7cfc04 |
function sets its argument as the seed for a new
|
|
Packit |
7cfc04 |
sequence of pseudo-random integers to be returned by
|
|
Packit |
7cfc04 |
.BR rand ().
|
|
Packit |
7cfc04 |
These sequences are repeatable by calling
|
|
Packit |
7cfc04 |
.BR srand ()
|
|
Packit |
7cfc04 |
with the same seed value.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
If no seed value is provided, the
|
|
Packit |
7cfc04 |
.BR rand ()
|
|
Packit |
7cfc04 |
function is automatically seeded with a value of 1.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
The function
|
|
Packit |
7cfc04 |
.BR rand ()
|
|
Packit |
7cfc04 |
is not reentrant, since it
|
|
Packit |
7cfc04 |
uses hidden state that is modified on each call.
|
|
Packit |
7cfc04 |
This might just be the seed value to be used by the next call,
|
|
Packit |
7cfc04 |
or it might be something more elaborate.
|
|
Packit |
7cfc04 |
In order to get reproducible behavior in a threaded
|
|
Packit |
7cfc04 |
application, this state must be made explicit;
|
|
Packit |
7cfc04 |
this can be done using the reentrant function
|
|
Packit |
7cfc04 |
.BR rand_r ().
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
Like
|
|
Packit |
7cfc04 |
.BR rand (),
|
|
Packit |
7cfc04 |
.BR rand_r ()
|
|
Packit |
7cfc04 |
returns a pseudo-random integer in the range [0,\ \fBRAND_MAX\fR].
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
.I seedp
|
|
Packit |
7cfc04 |
argument is a pointer to an
|
|
Packit |
7cfc04 |
.IR "unsigned int"
|
|
Packit |
7cfc04 |
that is used to store state between calls.
|
|
Packit |
7cfc04 |
If
|
|
Packit |
7cfc04 |
.BR rand_r ()
|
|
Packit |
7cfc04 |
is called with the same initial value for the integer pointed to by
|
|
Packit |
7cfc04 |
.IR seedp ,
|
|
Packit |
7cfc04 |
and that value is not modified between calls,
|
|
Packit |
7cfc04 |
then the same pseudo-random sequence will result.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
The value pointed to by the
|
|
Packit |
7cfc04 |
.I seedp
|
|
Packit |
7cfc04 |
argument of
|
|
Packit |
7cfc04 |
.BR rand_r ()
|
|
Packit |
7cfc04 |
provides only a very small amount of state,
|
|
Packit |
7cfc04 |
so this function will be a weak pseudo-random generator.
|
|
Packit |
7cfc04 |
Try
|
|
Packit |
7cfc04 |
.BR drand48_r (3)
|
|
Packit |
7cfc04 |
instead.
|
|
Packit |
7cfc04 |
.SH RETURN VALUE
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
.BR rand ()
|
|
Packit |
7cfc04 |
and
|
|
Packit |
7cfc04 |
.BR rand_r ()
|
|
Packit |
7cfc04 |
functions return a value between 0 and
|
|
Packit |
7cfc04 |
.BR RAND_MAX
|
|
Packit |
7cfc04 |
(inclusive).
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
.BR srand ()
|
|
Packit |
7cfc04 |
function returns no value.
|
|
Packit |
7cfc04 |
.SH ATTRIBUTES
|
|
Packit |
7cfc04 |
For an explanation of the terms used in this section, see
|
|
Packit |
7cfc04 |
.BR attributes (7).
|
|
Packit |
7cfc04 |
.TS
|
|
Packit |
7cfc04 |
allbox;
|
|
Packit |
7cfc04 |
lbw25 lb lb
|
|
Packit |
7cfc04 |
l l l.
|
|
Packit |
7cfc04 |
Interface Attribute Value
|
|
Packit |
7cfc04 |
T{
|
|
Packit |
7cfc04 |
.BR rand (),
|
|
Packit |
7cfc04 |
.BR rand_r (),
|
|
Packit |
7cfc04 |
.BR srand ()
|
|
Packit |
7cfc04 |
T} Thread safety MT-Safe
|
|
Packit |
7cfc04 |
.TE
|
|
Packit |
7cfc04 |
.SH CONFORMING TO
|
|
Packit |
7cfc04 |
The functions
|
|
Packit |
7cfc04 |
.BR rand ()
|
|
Packit |
7cfc04 |
and
|
|
Packit |
7cfc04 |
.BR srand ()
|
|
Packit |
7cfc04 |
conform to SVr4, 4.3BSD, C89, C99, POSIX.1-2001.
|
|
Packit |
7cfc04 |
The function
|
|
Packit |
7cfc04 |
.BR rand_r ()
|
|
Packit |
7cfc04 |
is from POSIX.1-2001.
|
|
Packit |
7cfc04 |
POSIX.1-2008 marks
|
|
Packit |
7cfc04 |
.BR rand_r ()
|
|
Packit |
7cfc04 |
as obsolete.
|
|
Packit |
7cfc04 |
.SH NOTES
|
|
Packit |
7cfc04 |
The versions of
|
|
Packit |
7cfc04 |
.BR rand ()
|
|
Packit |
7cfc04 |
and
|
|
Packit |
7cfc04 |
.BR srand ()
|
|
Packit |
7cfc04 |
in the Linux C Library use the same random number generator as
|
|
Packit |
7cfc04 |
.BR random (3)
|
|
Packit |
7cfc04 |
and
|
|
Packit |
7cfc04 |
.BR srandom (3),
|
|
Packit |
7cfc04 |
so the lower-order bits should be as random as the higher-order bits.
|
|
Packit |
7cfc04 |
However, on older
|
|
Packit |
7cfc04 |
.BR rand ()
|
|
Packit |
7cfc04 |
implementations, and on current implementations on different systems,
|
|
Packit |
7cfc04 |
the lower-order bits are much less random than the higher-order bits.
|
|
Packit |
7cfc04 |
Do not use this function in applications intended to be portable
|
|
Packit |
7cfc04 |
when good randomness is needed.
|
|
Packit |
7cfc04 |
(Use
|
|
Packit |
7cfc04 |
.BR random (3)
|
|
Packit |
7cfc04 |
instead.)
|
|
Packit |
7cfc04 |
.SH EXAMPLE
|
|
Packit |
7cfc04 |
POSIX.1-2001 gives the following example of an implementation of
|
|
Packit |
7cfc04 |
.BR rand ()
|
|
Packit |
7cfc04 |
and
|
|
Packit |
7cfc04 |
.BR srand (),
|
|
Packit |
7cfc04 |
possibly useful when one needs the same sequence on two different machines.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.in +4n
|
|
Packit |
7cfc04 |
.EX
|
|
Packit |
7cfc04 |
static unsigned long next = 1;
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
/* RAND_MAX assumed to be 32767 */
|
|
Packit |
7cfc04 |
int myrand(void) {
|
|
Packit |
7cfc04 |
next = next * 1103515245 + 12345;
|
|
Packit |
7cfc04 |
return((unsigned)(next/65536) % 32768);
|
|
Packit |
7cfc04 |
}
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
void mysrand(unsigned int seed) {
|
|
Packit |
7cfc04 |
next = seed;
|
|
Packit |
7cfc04 |
}
|
|
Packit |
7cfc04 |
.EE
|
|
Packit |
7cfc04 |
.in
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
The following program can be used to display the
|
|
Packit |
7cfc04 |
pseudo-random sequence produced by
|
|
Packit |
7cfc04 |
.BR rand ()
|
|
Packit |
7cfc04 |
when given a particular seed.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.in +4n
|
|
Packit |
7cfc04 |
.EX
|
|
Packit |
7cfc04 |
#include <stdlib.h>
|
|
Packit |
7cfc04 |
#include <stdio.h>
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
int
|
|
Packit |
7cfc04 |
main(int argc, char *argv[])
|
|
Packit |
7cfc04 |
{
|
|
Packit |
7cfc04 |
int j, r, nloops;
|
|
Packit |
7cfc04 |
unsigned int seed;
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
if (argc != 3) {
|
|
Packit |
7cfc04 |
fprintf(stderr, "Usage: %s <seed> <nloops>\\n", argv[0]);
|
|
Packit |
7cfc04 |
exit(EXIT_FAILURE);
|
|
Packit |
7cfc04 |
}
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
seed = atoi(argv[1]);
|
|
Packit |
7cfc04 |
nloops = atoi(argv[2]);
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
srand(seed);
|
|
Packit |
7cfc04 |
for (j = 0; j < nloops; j++) {
|
|
Packit |
7cfc04 |
r = rand();
|
|
Packit |
7cfc04 |
printf("%d\\n", r);
|
|
Packit |
7cfc04 |
}
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
exit(EXIT_SUCCESS);
|
|
Packit |
7cfc04 |
}
|
|
Packit |
7cfc04 |
.EE
|
|
Packit |
7cfc04 |
.in
|
|
Packit |
7cfc04 |
.SH SEE ALSO
|
|
Packit |
7cfc04 |
.BR drand48 (3),
|
|
Packit |
7cfc04 |
.BR random (3)
|
|
Packit |
7cfc04 |
.SH COLOPHON
|
|
Packit |
7cfc04 |
This page is part of release 4.15 of the Linux
|
|
Packit |
7cfc04 |
.I man-pages
|
|
Packit |
7cfc04 |
project.
|
|
Packit |
7cfc04 |
A description of the project,
|
|
Packit |
7cfc04 |
information about reporting bugs,
|
|
Packit |
7cfc04 |
and the latest version of this page,
|
|
Packit |
7cfc04 |
can be found at
|
|
Packit |
7cfc04 |
\%https://www.kernel.org/doc/man\-pages/.
|