選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

121 行
5.2KB

  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>rand (The Red Hat newlib C Library)</title>
  7. <meta name="description" content="rand (The Red Hat newlib C Library)">
  8. <meta name="keywords" content="rand (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="random.html#random" rel="next" title="random">
  17. <link href="qsort.html#qsort" rel="prev" title="qsort">
  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="rand"></a>
  48. <div class="header">
  49. <p>
  50. Next: <a href="random.html#random" accesskey="n" rel="next">random</a>, Previous: <a href="qsort.html#qsort" accesskey="p" rel="prev">qsort</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="rand_002c-srand_002d_002d_002dpseudo_002drandom-numbers"></a>
  54. <h3 class="section">2.33 <code>rand</code>, <code>srand</code>&mdash;pseudo-random numbers</h3>
  55. <a name="index-rand"></a>
  56. <a name="index-srand"></a>
  57. <a name="index-rand_005fr"></a>
  58. <p><strong>Synopsis</strong>
  59. </p><div class="example">
  60. <pre class="example">#include &lt;stdlib.h&gt;
  61. int rand(void);
  62. void srand(unsigned int <var>seed</var>);
  63. int rand_r(unsigned int *<var>seed</var>);
  64. </pre></div>
  65. <p><strong>Description</strong><br>
  66. <code>rand</code> returns a different integer each time it is called; each
  67. integer is chosen by an algorithm designed to be unpredictable, so
  68. that you can use <code>rand</code> when you require a random number.
  69. The algorithm depends on a static variable called the &ldquo;random seed&rdquo;;
  70. starting with a given value of the random seed always produces the
  71. same sequence of numbers in successive calls to <code>rand</code>.
  72. </p>
  73. <p>You can set the random seed using <code>srand</code>; it does nothing beyond
  74. storing its argument in the static variable used by <code>rand</code>. You can
  75. exploit this to make the pseudo-random sequence less predictable, if
  76. you wish, by using some other unpredictable value (often the least
  77. significant parts of a time-varying value) as the random seed before
  78. beginning a sequence of calls to <code>rand</code>; or, if you wish to ensure
  79. (for example, while debugging) that successive runs of your program
  80. use the same &ldquo;random&rdquo; numbers, you can use <code>srand</code> to set the same
  81. random seed at the outset.
  82. </p>
  83. <br>
  84. <p><strong>Returns</strong><br>
  85. <code>rand</code> returns the next pseudo-random integer in sequence; it is a
  86. number between <code>0</code> and <code>RAND_MAX</code> (inclusive).
  87. </p>
  88. <p><code>srand</code> does not return a result.
  89. </p>
  90. <br>
  91. <p><strong>Notes</strong><br>
  92. <code>rand</code> and <code>srand</code> are unsafe for multi-threaded applications.
  93. <code>rand_r</code> is thread-safe and should be used instead.
  94. </p>
  95. <br>
  96. <p><strong>Portability</strong><br>
  97. <code>rand</code> is required by ANSI, but the algorithm for pseudo-random
  98. number generation is not specified; therefore, even if you use
  99. the same random seed, you cannot expect the same sequence of results
  100. on two different systems.
  101. </p>
  102. <p><code>rand</code> requires no supporting OS subroutines.
  103. </p>
  104. <br>
  105. <hr>
  106. <div class="header">
  107. <p>
  108. Next: <a href="random.html#random" accesskey="n" rel="next">random</a>, Previous: <a href="qsort.html#qsort" accesskey="p" rel="prev">qsort</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>
  109. </div>
  110. </body>
  111. </html>