You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

321 lines
9.4 KiB

8 years ago
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <meta name="theme-color" content="#375EAB">
  7. <title>rand - The Go Programming Language</title>
  8. <link type="text/css" rel="stylesheet" href="../../../lib/godoc/style.css">
  9. <link rel="stylesheet" href="../../../lib/godoc/jquery.treeview.css">
  10. <script type="text/javascript">window.initFuncs = [];</script>
  11. </head>
  12. <body>
  13. <div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
  14. ...
  15. </div><!-- #lowframe -->
  16. <div id="topbar" class="wide"><div class="container">
  17. <div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div>
  18. <div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div>
  19. <a href="index.html#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
  20. <form method="GET" action="http://localhost:6060/search">
  21. <div id="menu">
  22. <a href="http://localhost:6060/doc/">Documents</a>
  23. <a href="http://localhost:6060/pkg/">Packages</a>
  24. <a href="http://localhost:6060/project/">The Project</a>
  25. <a href="http://localhost:6060/help/">Help</a>
  26. <a href="http://localhost:6060/blog/">Blog</a>
  27. <input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
  28. </div>
  29. </form>
  30. </div></div>
  31. <div id="page" class="wide">
  32. <div class="container">
  33. <h1>Package rand</h1>
  34. <div id="nav"></div>
  35. <!--
  36. Copyright 2009 The Go Authors. All rights reserved.
  37. Use of this source code is governed by a BSD-style
  38. license that can be found in the LICENSE file.
  39. -->
  40. <!--
  41. Note: Static (i.e., not template-generated) href and id
  42. attributes start with "pkg-" to make it impossible for
  43. them to conflict with generated attributes (some of which
  44. correspond to Go identifiers).
  45. -->
  46. <script type='text/javascript'>
  47. document.ANALYSIS_DATA = null;
  48. document.CALLGRAPH = null;
  49. </script>
  50. <div id="short-nav">
  51. <dl>
  52. <dd><code>import "crypto/rand"</code></dd>
  53. </dl>
  54. <dl>
  55. <dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd>
  56. <dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd>
  57. <dd><a href="index.html#pkg-examples" class="examplesLink">Examples</a></dd>
  58. </dl>
  59. </div>
  60. <!-- The package's Name is printed as title by the top-level template -->
  61. <div id="pkg-overview" class="toggleVisible">
  62. <div class="collapsed">
  63. <h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
  64. </div>
  65. <div class="expanded">
  66. <h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
  67. <p>
  68. Package rand implements a cryptographically secure
  69. pseudorandom number generator.
  70. </p>
  71. </div>
  72. </div>
  73. <div id="pkg-index" class="toggleVisible">
  74. <div class="collapsed">
  75. <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
  76. </div>
  77. <div class="expanded">
  78. <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
  79. <!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
  80. <div id="manual-nav">
  81. <dl>
  82. <dd><a href="index.html#pkg-variables">Variables</a></dd>
  83. <dd><a href="index.html#Int">func Int(rand io.Reader, max *big.Int) (n *big.Int, err error)</a></dd>
  84. <dd><a href="index.html#Prime">func Prime(rand io.Reader, bits int) (p *big.Int, err error)</a></dd>
  85. <dd><a href="index.html#Read">func Read(b []byte) (n int, err error)</a></dd>
  86. </dl>
  87. </div><!-- #manual-nav -->
  88. <div id="pkg-examples">
  89. <h4>Examples</h4>
  90. <dl>
  91. <dd><a class="exampleLink" href="index.html#example_Read">Read</a></dd>
  92. </dl>
  93. </div>
  94. <h4>Package files</h4>
  95. <p>
  96. <span style="font-size:90%">
  97. <a href="http://localhost:6060/src/crypto/rand/eagain.go">eagain.go</a>
  98. <a href="http://localhost:6060/src/crypto/rand/rand.go">rand.go</a>
  99. <a href="http://localhost:6060/src/crypto/rand/rand_linux.go">rand_linux.go</a>
  100. <a href="http://localhost:6060/src/crypto/rand/rand_unix.go">rand_unix.go</a>
  101. <a href="http://localhost:6060/src/crypto/rand/util.go">util.go</a>
  102. </span>
  103. </p>
  104. </div><!-- .expanded -->
  105. </div><!-- #pkg-index -->
  106. <div id="pkg-callgraph" class="toggle" style="display: none">
  107. <div class="collapsed">
  108. <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
  109. </div> <!-- .expanded -->
  110. <div class="expanded">
  111. <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
  112. <p>
  113. In the call graph viewer below, each node
  114. is a function belonging to this package
  115. and its children are the functions it
  116. calls&mdash;perhaps dynamically.
  117. </p>
  118. <p>
  119. The root nodes are the entry points of the
  120. package: functions that may be called from
  121. outside the package.
  122. There may be non-exported or anonymous
  123. functions among them if they are called
  124. dynamically from another package.
  125. </p>
  126. <p>
  127. Click a node to visit that function's source code.
  128. From there you can visit its callers by
  129. clicking its declaring <code>func</code>
  130. token.
  131. </p>
  132. <p>
  133. Functions may be omitted if they were
  134. determined to be unreachable in the
  135. particular programs or tests that were
  136. analyzed.
  137. </p>
  138. <!-- Zero means show all package entry points. -->
  139. <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
  140. </div>
  141. </div> <!-- #pkg-callgraph -->
  142. <h2 id="pkg-variables">Variables</h2>
  143. <pre>var <span id="Reader">Reader</span> <a href="../../io/index.html">io</a>.<a href="../../io/index.html#Reader">Reader</a></pre>
  144. <p>
  145. Reader is a global, shared instance of a cryptographically
  146. strong pseudo-random generator.
  147. </p>
  148. <p>
  149. On Unix-like systems, Reader reads from /dev/urandom.
  150. On Linux, Reader uses getrandom(2) if available, /dev/urandom otherwise.
  151. On Windows systems, Reader uses the CryptGenRandom API.
  152. </p>
  153. <h2 id="Int">func <a href="http://localhost:6060/src/crypto/rand/util.go?s=3072:3134#L96">Int</a></h2>
  154. <pre>func Int(rand <a href="../../io/index.html">io</a>.<a href="../../io/index.html#Reader">Reader</a>, max *<a href="../../math/big/index.html">big</a>.<a href="../../math/big/index.html#Int">Int</a>) (n *<a href="../../math/big/index.html">big</a>.<a href="../../math/big/index.html#Int">Int</a>, err <a href="../../builtin/index.html#error">error</a>)</pre>
  155. <p>
  156. Int returns a uniform random value in [0, max). It panics if max &lt;= 0.
  157. </p>
  158. <h2 id="Prime">func <a href="http://localhost:6060/src/crypto/rand/util.go?s=1126:1186#L21">Prime</a></h2>
  159. <pre>func Prime(rand <a href="../../io/index.html">io</a>.<a href="../../io/index.html#Reader">Reader</a>, bits <a href="../../builtin/index.html#int">int</a>) (p *<a href="../../math/big/index.html">big</a>.<a href="../../math/big/index.html#Int">Int</a>, err <a href="../../builtin/index.html#error">error</a>)</pre>
  160. <p>
  161. Prime returns a number, p, of the given size, such that p is prime
  162. with high probability.
  163. Prime will return error for any error returned by rand.Read or if bits &lt; 2.
  164. </p>
  165. <h2 id="Read">func <a href="http://localhost:6060/src/crypto/rand/rand.go?s=714:752#L11">Read</a></h2>
  166. <pre>func Read(b []<a href="../../builtin/index.html#byte">byte</a>) (n <a href="../../builtin/index.html#int">int</a>, err <a href="../../builtin/index.html#error">error</a>)</pre>
  167. <p>
  168. Read is a helper function that calls Reader.Read using io.ReadFull.
  169. On return, n == len(b) if and only if err == nil.
  170. </p>
  171. <div id="example_Read" class="toggle">
  172. <div class="collapsed">
  173. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  174. </div>
  175. <div class="expanded">
  176. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  177. <p>This example reads 10 cryptographically secure pseudorandom numbers from
  178. rand.Reader and writes them to a byte slice.
  179. </p>
  180. <p>Code:</p>
  181. <pre class="code">c := 10
  182. b := make([]byte, c)
  183. _, err := rand.Read(b)
  184. if err != nil {
  185. fmt.Println(&#34;error:&#34;, err)
  186. return
  187. }
  188. <span class="comment">// The slice should now contain random bytes instead of only zeroes.</span>
  189. fmt.Println(bytes.Equal(b, make([]byte, c)))
  190. <span class="comment"></pre>
  191. <p>Output:</p>
  192. <pre class="output">false
  193. </pre>
  194. </div>
  195. </div>
  196. <div id="footer">
  197. Build version go1.6.<br>
  198. Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
  199. the content of this page is licensed under the
  200. Creative Commons Attribution 3.0 License,
  201. and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
  202. <a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
  203. <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
  204. </div>
  205. </div><!-- .container -->
  206. </div><!-- #page -->
  207. <!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
  208. <script type="text/javascript" src="../../../lib/godoc/jquery.js"></script>
  209. <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.js"></script>
  210. <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.edit.js"></script>
  211. <script type="text/javascript" src="../../../lib/godoc/godocs.js"></script>
  212. </body>
  213. </html>