logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

initstate.3p (5576B)


  1. '\" et
  2. .TH INITSTATE "3P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
  3. .\"
  4. .SH PROLOG
  5. This manual page is part of the POSIX Programmer's Manual.
  6. The Linux implementation of this interface may differ (consult
  7. the corresponding Linux manual page for details of Linux behavior),
  8. or the interface may not be implemented on Linux.
  9. .\"
  10. .SH NAME
  11. initstate,
  12. random,
  13. setstate,
  14. srandom
  15. \(em pseudo-random number functions
  16. .SH SYNOPSIS
  17. .LP
  18. .nf
  19. #include <stdlib.h>
  20. .P
  21. char *initstate(unsigned \fIseed\fP, char *\fIstate\fP, size_t \fIsize\fP);
  22. long random(void);
  23. char *setstate(char *\fIstate\fP);
  24. void srandom(unsigned \fIseed\fP);
  25. .fi
  26. .SH DESCRIPTION
  27. The
  28. \fIrandom\fR()
  29. function shall use a non-linear additive feedback random-number
  30. generator employing a default state array size of 31
  31. .BR long
  32. integers to return successive pseudo-random numbers in the range from 0
  33. to 2\u\s-331\s+3\d\-1. The period of this random-number generator is
  34. approximately 16 x (2\s-3\u31\d\s+3\-\fR1). The size of the state
  35. array determines the period of the random-number generator. Increasing
  36. the state array size shall increase the period.
  37. .P
  38. With 256 bytes of state information, the period of the random-number
  39. generator shall be greater than 2\s-3\u69\d\s+3.
  40. .P
  41. Like
  42. \fIrand\fR(),
  43. \fIrandom\fR()
  44. shall produce by default a sequence of numbers that can be duplicated
  45. by calling
  46. \fIsrandom\fR()
  47. with 1 as the seed.
  48. .P
  49. The
  50. \fIsrandom\fR()
  51. function shall initialize the current state array using the value of
  52. .IR seed .
  53. .P
  54. The
  55. \fIinitstate\fR()
  56. and
  57. \fIsetstate\fR()
  58. functions handle restarting and changing random-number generators. The
  59. \fIinitstate\fR()
  60. function allows a state array, pointed to by the
  61. .IR state
  62. argument, to be initialized for future use. The
  63. .IR size
  64. argument, which specifies the size in bytes of the state array, shall
  65. be used by
  66. \fIinitstate\fR()
  67. to decide what type of random-number generator to use; the larger the
  68. state array, the more random the numbers. Values for the amount of
  69. state information are 8, 32, 64, 128, and 256 bytes. Other values
  70. greater than 8 bytes are rounded down to the nearest one of these
  71. values. If
  72. \fIinitstate\fR()
  73. is called with 8\(<=\fIsize\fR<32, then
  74. \fIrandom\fR()
  75. shall use a simple linear congruential random number generator. The
  76. .IR seed
  77. argument specifies a starting point for the random-number sequence and
  78. provides for restarting at the same point. The
  79. \fIinitstate\fR()
  80. function shall return a pointer to the previous state information array.
  81. .P
  82. If
  83. \fIinitstate\fR()
  84. has not been called, then
  85. \fIrandom\fR()
  86. shall behave as though
  87. \fIinitstate\fR()
  88. had been called with
  89. .IR seed =1
  90. and
  91. .IR size =128.
  92. .P
  93. Once a state has been initialized,
  94. \fIsetstate\fR()
  95. allows switching between state arrays. The array defined by the
  96. .IR state
  97. argument shall be used for further random-number generation until
  98. \fIinitstate\fR()
  99. is called or
  100. \fIsetstate\fR()
  101. is called again. The
  102. \fIsetstate\fR()
  103. function shall return a pointer to the previous state array.
  104. .SH "RETURN VALUE"
  105. If
  106. \fIinitstate\fR()
  107. is called with
  108. .IR size
  109. less than 8, it shall return NULL.
  110. .P
  111. The
  112. \fIrandom\fR()
  113. function shall return the generated pseudo-random number.
  114. .P
  115. The
  116. \fIsrandom\fR()
  117. function shall not return a value.
  118. .P
  119. Upon successful completion,
  120. \fIinitstate\fR()
  121. and
  122. \fIsetstate\fR()
  123. shall return a pointer to the previous state array; otherwise, a null
  124. pointer shall be returned.
  125. .SH ERRORS
  126. No errors are defined.
  127. .LP
  128. .IR "The following sections are informative."
  129. .SH EXAMPLES
  130. None.
  131. .SH "APPLICATION USAGE"
  132. After initialization, a state array can be restarted at a different
  133. point in one of two ways:
  134. .IP " 1." 4
  135. The
  136. \fIinitstate\fR()
  137. function can be used, with the desired seed, state array, and size of
  138. the array.
  139. .IP " 2." 4
  140. The
  141. \fIsetstate\fR()
  142. function, with the desired state, can be used, followed by
  143. \fIsrandom\fR()
  144. with the desired seed. The advantage of using both of these functions
  145. is that the size of the state array does not have to be saved once it
  146. is initialized.
  147. .P
  148. Although some implementations of
  149. \fIrandom\fR()
  150. have written messages to standard error, such implementations do not
  151. conform to POSIX.1\(hy2008.
  152. .P
  153. Issue 5 restored the historical behavior of this function.
  154. .P
  155. Threaded applications should use
  156. \fIerand48\fR(),
  157. \fInrand48\fR(),
  158. or
  159. \fIjrand48\fR()
  160. instead of
  161. \fIrandom\fR()
  162. when an independent random number sequence in multiple threads is
  163. required.
  164. .P
  165. These functions should be avoided whenever non-trivial requirements
  166. (including safety) have to be fulfilled.
  167. .SH RATIONALE
  168. None.
  169. .SH "FUTURE DIRECTIONS"
  170. None.
  171. .SH "SEE ALSO"
  172. .IR "\fIdrand48\fR\^(\|)",
  173. .IR "\fIrand\fR\^(\|)"
  174. .P
  175. The Base Definitions volume of POSIX.1\(hy2017,
  176. .IR "\fB<stdlib.h>\fP"
  177. .\"
  178. .SH COPYRIGHT
  179. Portions of this text are reprinted and reproduced in electronic form
  180. from IEEE Std 1003.1-2017, Standard for Information Technology
  181. -- Portable Operating System Interface (POSIX), The Open Group Base
  182. Specifications Issue 7, 2018 Edition,
  183. Copyright (C) 2018 by the Institute of
  184. Electrical and Electronics Engineers, Inc and The Open Group.
  185. In the event of any discrepancy between this version and the original IEEE and
  186. The Open Group Standard, the original IEEE and The Open Group Standard
  187. is the referee document. The original Standard can be obtained online at
  188. http://www.opengroup.org/unix/online.html .
  189. .PP
  190. Any typographical or formatting errors that appear
  191. in this page are most likely
  192. to have been introduced during the conversion of the source files to
  193. man page format. To report such errors, see
  194. https://www.kernel.org/doc/man-pages/reporting_bugs.html .