Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

3 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <title>random (The Red Hat newlib C Library)</title>
  7. <meta name="description" content="random (The Red Hat newlib C Library)">
  8. <meta name="keywords" content="random (The Red Hat newlib C Library)">
  9. <meta name="resource-type" content="document">
  10. <meta name="distribution" content="global">
  11. <meta name="Generator" content="makeinfo">
  12. <link href="index.html#Top" rel="start" title="Top">
  13. <link href="Document-Index.html#Document-Index" rel="index" title="Document Index">
  14. <link href="Document-Index.html#SEC_Contents" rel="contents" title="Table of Contents">
  15. <link href="Stdlib.html#Stdlib" rel="up" title="Stdlib">
  16. <link href="rand48.html#rand48" rel="next" title="rand48">
  17. <link href="rand.html#rand" rel="prev" title="rand">
  18. <style type="text/css">
  19. <!--
  20. a.summary-letter {text-decoration: none}
  21. blockquote.indentedblock {margin-right: 0em}
  22. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  23. blockquote.smallquotation {font-size: smaller}
  24. div.display {margin-left: 3.2em}
  25. div.example {margin-left: 3.2em}
  26. div.lisp {margin-left: 3.2em}
  27. div.smalldisplay {margin-left: 3.2em}
  28. div.smallexample {margin-left: 3.2em}
  29. div.smalllisp {margin-left: 3.2em}
  30. kbd {font-style: oblique}
  31. pre.display {font-family: inherit}
  32. pre.format {font-family: inherit}
  33. pre.menu-comment {font-family: serif}
  34. pre.menu-preformatted {font-family: serif}
  35. pre.smalldisplay {font-family: inherit; font-size: smaller}
  36. pre.smallexample {font-size: smaller}
  37. pre.smallformat {font-family: inherit; font-size: smaller}
  38. pre.smalllisp {font-size: smaller}
  39. span.nolinebreak {white-space: nowrap}
  40. span.roman {font-family: initial; font-weight: normal}
  41. span.sansserif {font-family: sans-serif; font-weight: normal}
  42. ul.no-bullet {list-style: none}
  43. -->
  44. </style>
  45. </head>
  46. <body lang="en">
  47. <a name="random"></a>
  48. <div class="header">
  49. <p>
  50. Next: <a href="rand48.html#rand48" accesskey="n" rel="next">rand48</a>, Previous: <a href="rand.html#rand" accesskey="p" rel="prev">rand</a>, Up: <a href="Stdlib.html#Stdlib" accesskey="u" rel="up">Stdlib</a> &nbsp; [<a href="Document-Index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Document-Index.html#Document-Index" title="Index" rel="index">Index</a>]</p>
  51. </div>
  52. <hr>
  53. <a name="random_002c-srandom_002d_002d_002dpseudo_002drandom-numbers"></a>
  54. <h3 class="section">2.34 <code>random</code>, <code>srandom</code>&mdash;pseudo-random numbers</h3>
  55. <a name="index-random"></a>
  56. <a name="index-srandom"></a>
  57. <p><strong>Synopsis</strong>
  58. </p><div class="example">
  59. <pre class="example">#define _XOPEN_SOURCE 500
  60. #include &lt;stdlib.h&gt;
  61. long int random(void);
  62. void srandom(unsigned int <var>seed</var>);
  63. </pre></div>
  64. <p><strong>Description</strong><br>
  65. <code>random</code> returns a different integer each time it is called; each
  66. integer is chosen by an algorithm designed to be unpredictable, so
  67. that you can use <code>random</code> when you require a random number.
  68. The algorithm depends on a static variable called the &ldquo;random seed&rdquo;;
  69. starting with a given value of the random seed always produces the
  70. same sequence of numbers in successive calls to <code>random</code>.
  71. </p>
  72. <p>You can set the random seed using <code>srandom</code>; it does nothing beyond
  73. storing its argument in the static variable used by <code>rand</code>. You can
  74. exploit this to make the pseudo-random sequence less predictable, if
  75. you wish, by using some other unpredictable value (often the least
  76. significant parts of a time-varying value) as the random seed before
  77. beginning a sequence of calls to <code>rand</code>; or, if you wish to ensure
  78. (for example, while debugging) that successive runs of your program
  79. use the same &ldquo;random&rdquo; numbers, you can use <code>srandom</code> to set the same
  80. random seed at the outset.
  81. </p>
  82. <br>
  83. <p><strong>Returns</strong><br>
  84. <code>random</code> returns the next pseudo-random integer in sequence; it is a
  85. number between <code>0</code> and <code>RAND_MAX</code> (inclusive).
  86. </p>
  87. <p><code>srandom</code> does not return a result.
  88. </p>
  89. <br>
  90. <p><strong>Notes</strong><br>
  91. <code>random</code> and <code>srandom</code> are unsafe for multi-threaded applications.
  92. </p>
  93. <p>_XOPEN_SOURCE may be any value &gt;= 500.
  94. </p>
  95. <br>
  96. <p><strong>Portability</strong><br>
  97. <code>random</code> is required by XSI. This implementation uses the same
  98. algorithm as <code>rand</code>.
  99. </p>
  100. <p><code>random</code> requires no supporting OS subroutines.
  101. </p>
  102. <br>
  103. </body>
  104. </html>