123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807 |
- <?xml version="1.0" encoding="utf-8" ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <!-- This file is generated by Nim. -->
- <html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>std/random</title>
- <!-- Google fonts -->
- <link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
- <link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
- <!-- Favicon -->
- <link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAUAAAAF////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAIAAABbAAAAlQAAAKIAAACbAAAAmwAAAKIAAACVAAAAWwAAAAL///8A////AP///wD///8A////AAAAABQAAADAAAAAYwAAAA3///8A////AP///wD///8AAAAADQAAAGMAAADAAAAAFP///wD///8A////AP///wAAAACdAAAAOv///wD///8A////AP///wD///8A////AP///wD///8AAAAAOgAAAJ3///8A////AP///wAAAAAnAAAAcP///wAAAAAoAAAASv///wD///8A////AP///wAAAABKAAAAKP///wAAAABwAAAAJ////wD///8AAAAAgQAAABwAAACIAAAAkAAAAJMAAACtAAAAFQAAABUAAACtAAAAkwAAAJAAAACIAAAAHAAAAIH///8A////AAAAAKQAAACrAAAAaP///wD///8AAAAARQAAANIAAADSAAAARf///wD///8AAAAAaAAAAKsAAACk////AAAAADMAAACcAAAAnQAAABj///8A////AP///wAAAAAYAAAAGP///wD///8A////AAAAABgAAACdAAAAnAAAADMAAAB1AAAAwwAAAP8AAADpAAAAsQAAAE4AAAAb////AP///wAAAAAbAAAATgAAALEAAADpAAAA/wAAAMMAAAB1AAAAtwAAAOkAAAD/AAAA/wAAAP8AAADvAAAA3gAAAN4AAADeAAAA3gAAAO8AAAD/AAAA/wAAAP8AAADpAAAAtwAAAGUAAAA/AAAA3wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAADfAAAAPwAAAGX///8A////AAAAAEgAAADtAAAAvwAAAL0AAADGAAAA7wAAAO8AAADGAAAAvQAAAL8AAADtAAAASP///wD///8A////AP///wD///8AAAAAO////wD///8A////AAAAAIcAAACH////AP///wD///8AAAAAO////wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A//8AAP//AAD4HwAA7/cAAN/7AAD//wAAoYUAAJ55AACf+QAAh+EAAAAAAADAAwAA4AcAAP5/AAD//wAA//8AAA=="/>
- <link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4QQQEwksSS9ZWwAAAk1JREFUWMPtll2ITVEUx39nn/O7Y5qR8f05wtCUUr6ZIS++8pEnkZInPImneaCQ5METNdOkeFBKUhMPRIkHKfEuUZSUlGlKPN2TrgfncpvmnntnmlEyq1Z7t89/rf9a6+y99oZxGZf/XeIq61EdtgKXgdXA0xrYAvBjOIF1AI9zvjcC74BSpndrJPkBWDScTF8Aa4E3wDlgHbASaANmVqlcCnwHvgDvgVfAJ+AikAAvgfVZwLnSVZHZaOuKoQi3ZOMi4NkYkpe1p4J7A8BpYAD49hfIy/oqG0+hLomiKP2L5L+1ubn5115S+3OAn4EnwBlgMzCjyt6ZAnQCJ4A7wOs88iRJHvw50HoujuPBoCKwHWiosy8MdfZnAdcHk8dxXFJ3VQbQlCTJvRBCGdRbD4M6uc5glpY3eAihpN5S5w12diSEcCCEcKUO4ljdr15T76ur1FDDLIQQ3qv71EdDOe3Kxj3leRXyk+pxdWnFWod6Wt2bY3de3aSuUHcPBVimHs7mK9WrmeOF6lR1o9qnzskh2ar2qm1qizpfXaPeVGdlmGN5pb09qMxz1Xb1kLqgzn1RyH7JUXW52lr5e/Kqi9qpto7V1atuUzfnARrV7jEib1T76gG2qxdGmXyiekkt1GswPTtek0aBfJp6YySGBfWg2tPQ0FAYgf1stUfdmdcjarbYJEniKIq6gY/Aw+zWHAC+p2labGpqiorFYgGYCEzN7oQdQClN07O1/EfDyGgC0ALMBdYAi4FyK+4H3gLPsxfR1zRNi+NP7nH5J+QntnXe5B5mpfQAAAAASUVORK5CYII=">
- <!-- CSS -->
- <link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
- <!-- JS -->
- <script type="text/javascript" src="dochack.js?v=2.3.1"></script>
- </head>
- <body>
- <div class="document" id="documentId">
- <div class="container">
- <h1 class="title">std/random</h1>
- <div class="row">
- <div class="three columns">
- <div class="theme-select-wrapper">
- <label for="theme-select">Theme: </label>
- <select id="theme-select" onchange="setTheme(this.value)">
- <option value="auto">🌗 Match OS</option>
- <option value="dark">🌑 Dark</option>
- <option value="light">🌕 Light</option>
- </select>
- </div>
- <div id="global-links">
- <ul class="simple-boot">
- <li><a href="manual.html">Manual</a></li>
- <li><a href="lib.html">Standard library</a></li>
- <li> <a id="indexLink" href="theindex.html">Index</a></li>
- <li><a href="compiler/theindex.html">Compiler docs</a></li>
- <li><a href="https://nim-lang.github.io/fusion/theindex.html">Fusion docs</a></li>
- <li><a href="https://nim-lang.github.io/Nim/">devel</a>, <a href="https://nim-lang.org/documentation.html">stable</a></li>
- </ul>
- </div>
- <div id="searchInputDiv">
- Search: <input type="search" id="searchInput"
- oninput="search()" />
- </div>
-
- <ul class="simple simple-toc" id="toc-list">
- <li><a class="reference" id="basic-usage_toc" href="#basic-usage">Basic usage</a></li>
- <li><a class="reference" id="see-also_toc" href="#see-also">See also</a></li>
- <li>
- <a class="reference reference-toplevel" href="#6" id="56">Imports</a>
- </li>
- <li>
- <details open>
- <summary><a class="reference reference-toplevel" href="#7" id="57">Types</a></summary>
- <ul class="simple simple-toc-section">
- <li><a class="reference" href="#Rand" title="Rand = object">Rand</a></li>
- </ul>
- </details>
- </li>
- <li>
- <details open>
- <summary><a class="reference reference-toplevel" href="#12" id="62">Procs</a></summary>
- <ul class="simple simple-toc-section">
- <ul class="simple nested-toc-section">gauss
- <li><a class="reference" href="#gauss%2Cfloat%2Cfloat" title="gauss(mu = 0.0; sigma = 1.0): float">gauss(mu = 0.0; sigma = 1.0): float</a></li>
- <li><a class="reference" href="#gauss%2CRand%2Cfloat%2Cfloat" title="gauss(r: var Rand; mu = 0.0; sigma = 1.0): float">gauss(r: var Rand; mu = 0.0; sigma = 1.0): float</a></li>
- </ul>
- <ul class="simple nested-toc-section">initRand
- <li><a class="reference" href="#initRand" title="initRand(): Rand">initRand(): Rand</a></li>
- <li><a class="reference" href="#initRand%2Cint64" title="initRand(seed: int64): Rand">initRand(seed: int64): Rand</a></li>
- </ul>
- <ul class="simple nested-toc-section">next
- <li><a class="reference" href="#next%2CRand" title="next(r: var Rand): uint64">next(r: var Rand): uint64</a></li>
- </ul>
- <ul class="simple nested-toc-section">rand
- <li><a class="reference" href="#rand%2Cfloat" title="rand(max: float): float">rand(max: float): float</a></li>
- <li><a class="reference" href="#rand%2Cint" title="rand(max: int): int">rand(max: int): int</a></li>
- <li><a class="reference" href="#rand%2CRand%2CNatural" title="rand(r: var Rand; max: Natural): int">rand(r: var Rand; max: Natural): int</a></li>
- <li><a class="reference" href="#rand%2CRand%2Crange%5B%5D" title="rand(r: var Rand; max: range[0.0 .. high(float)]): float">rand(r: var Rand; max: range[0.0 .. high(float)]): float</a></li>
- <li><a class="reference" href="#rand%2CRand%2CHSlice%5BT%3A%20Ordinal%20or%20float%20or%20float32%20or%20float64%2CT%3A%20Ordinal%20or%20float%20or%20float32%20or%20float64%5D" title="rand[T: Ordinal or SomeFloat](r: var Rand; x: HSlice[T, T]): T">rand[T: Ordinal or SomeFloat](r: var Rand; x: HSlice[T, T]): T</a></li>
- <li><a class="reference" href="#rand%2CHSlice%5BT%3A%20Ordinal%20or%20float%20or%20float32%20or%20float64%2CT%3A%20Ordinal%20or%20float%20or%20float32%20or%20float64%5D" title="rand[T: Ordinal or SomeFloat](x: HSlice[T, T]): T">rand[T: Ordinal or SomeFloat](x: HSlice[T, T]): T</a></li>
- <li><a class="reference" href="#rand%2CRand%2Ctypedesc%5BT%5D" title="rand[T: Ordinal](r: var Rand; t: typedesc[T]): T">rand[T: Ordinal](r: var Rand; t: typedesc[T]): T</a></li>
- <li><a class="reference" href="#rand%2Ctypedesc%5BT%5D" title="rand[T: Ordinal](t: typedesc[T]): T">rand[T: Ordinal](t: typedesc[T]): T</a></li>
- </ul>
- <ul class="simple nested-toc-section">randomize
- <li><a class="reference" href="#randomize" title="randomize()">randomize()</a></li>
- <li><a class="reference" href="#randomize%2Cint64" title="randomize(seed: int64)">randomize(seed: int64)</a></li>
- </ul>
- <ul class="simple nested-toc-section">sample
- <li><a class="reference" href="#sample%2CopenArray%5BT%5D%2CopenArray%5BU%5D" title="sample[T, U](a: openArray[T]; cdf: openArray[U]): T">sample[T, U](a: openArray[T]; cdf: openArray[U]): T</a></li>
- <li><a class="reference" href="#sample%2CRand%2CopenArray%5BT%5D%2CopenArray%5BU%5D" title="sample[T, U](r: var Rand; a: openArray[T]; cdf: openArray[U]): T">sample[T, U](r: var Rand; a: openArray[T]; cdf: openArray[U]): T</a></li>
- <li><a class="reference" href="#sample%2CopenArray%5BT%5D" title="sample[T](a: openArray[T]): lent T">sample[T](a: openArray[T]): lent T</a></li>
- <li><a class="reference" href="#sample%2CRand%2CopenArray%5BT%5D" title="sample[T](r: var Rand; a: openArray[T]): T">sample[T](r: var Rand; a: openArray[T]): T</a></li>
- <li><a class="reference" href="#sample%2CRand%2Cset%5BT%5D" title="sample[T](r: var Rand; s: set[T]): T">sample[T](r: var Rand; s: set[T]): T</a></li>
- <li><a class="reference" href="#sample%2Cset%5BT%5D" title="sample[T](s: set[T]): T">sample[T](s: set[T]): T</a></li>
- </ul>
- <ul class="simple nested-toc-section">shuffle
- <li><a class="reference" href="#shuffle%2CRand%2CopenArray%5BT%5D" title="shuffle[T](r: var Rand; x: var openArray[T])">shuffle[T](r: var Rand; x: var openArray[T])</a></li>
- <li><a class="reference" href="#shuffle%2CopenArray%5BT%5D" title="shuffle[T](x: var openArray[T])">shuffle[T](x: var openArray[T])</a></li>
- </ul>
- <ul class="simple nested-toc-section">skipRandomNumbers
- <li><a class="reference" href="#skipRandomNumbers%2CRand" title="skipRandomNumbers(s: var Rand)">skipRandomNumbers(s: var Rand)</a></li>
- </ul>
- </ul>
- </details>
- </li>
- <li>
- <details open>
- <summary><a class="reference reference-toplevel" href="#18" id="68">Templates</a></summary>
- <ul class="simple simple-toc-section">
- <ul class="simple nested-toc-section">randState
- <li><a class="reference" href="#randState.t" title="randState(): untyped">randState(): untyped</a></li>
- </ul>
- </ul>
- </details>
- </li>
- </ul>
- </div>
- <div class="nine columns" id="content">
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L1" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L1" class="link-seesrc" target="_blank" >Edit</a>
- <div id="tocRoot"></div>
-
- <p class="module-desc"><p>Nim's standard random number generator (RNG).</p>
- <p>Its implementation is based on the <tt class="docutils literal"><span class="pre"><span class="Identifier">xoroshiro128</span><span class="Operator">+</span></span></tt> (xor/rotate/shift/rotate) library.</p>
- <ul class="simple"><li>More information: <a class="reference external" href="https://xoroshiro.di.unimi.it">https://xoroshiro.di.unimi.it</a></li>
- <li>C implementation: <a class="reference external" href="https://xoroshiro.di.unimi.it/xoroshiro128plus.c">https://xoroshiro.di.unimi.it/xoroshiro128plus.c</a></li>
- </ul>
- <p><strong>Do not use this module for cryptographic purposes!</strong></p>
- <h1><a class="toc-backref" id="basic-usage" href="#basic-usage">Basic usage</a></h1>
- <p><strong class="examples_text">Example:</strong></p>
- <pre class="listing"><span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">random</span>
- <span class="Comment"># Call randomize() once to initialize the default random number generator.</span>
- <span class="Comment"># If this is not called, the same results will occur every time these</span>
- <span class="Comment"># examples are run.</span>
- <span class="Identifier">randomize</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
- <span class="Comment"># Pick a number in 0..100.</span>
- <span class="Keyword">let</span> <span class="Identifier">num</span> <span class="Operator">=</span> <span class="Identifier">rand</span><span class="Punctuation">(</span><span class="DecNumber">100</span><span class="Punctuation">)</span>
- <span class="Identifier">doAssert</span> <span class="Identifier">num</span> <span class="Keyword">in</span> <span class="FloatNumber">0.</span><span class="Operator">.</span><span class="DecNumber">100</span>
- <span class="Comment"># Roll a six-sided die.</span>
- <span class="Keyword">let</span> <span class="Identifier">roll</span> <span class="Operator">=</span> <span class="Identifier">rand</span><span class="Punctuation">(</span><span class="FloatNumber">1.</span><span class="Operator">.</span><span class="DecNumber">6</span><span class="Punctuation">)</span>
- <span class="Identifier">doAssert</span> <span class="Identifier">roll</span> <span class="Keyword">in</span> <span class="FloatNumber">1.</span><span class="Operator">.</span><span class="DecNumber">6</span>
- <span class="Comment"># Pick a marble from a bag.</span>
- <span class="Keyword">let</span> <span class="Identifier">marbles</span> <span class="Operator">=</span> <span class="Punctuation">[</span><span class="StringLit">"red"</span><span class="Punctuation">,</span> <span class="StringLit">"blue"</span><span class="Punctuation">,</span> <span class="StringLit">"green"</span><span class="Punctuation">,</span> <span class="StringLit">"yellow"</span><span class="Punctuation">,</span> <span class="StringLit">"purple"</span><span class="Punctuation">]</span>
- <span class="Keyword">let</span> <span class="Identifier">pick</span> <span class="Operator">=</span> <span class="Identifier">sample</span><span class="Punctuation">(</span><span class="Identifier">marbles</span><span class="Punctuation">)</span>
- <span class="Identifier">doAssert</span> <span class="Identifier">pick</span> <span class="Keyword">in</span> <span class="Identifier">marbles</span>
- <span class="Comment"># Shuffle some cards.</span>
- <span class="Keyword">var</span> <span class="Identifier">cards</span> <span class="Operator">=</span> <span class="Punctuation">[</span><span class="StringLit">"Ace"</span><span class="Punctuation">,</span> <span class="StringLit">"King"</span><span class="Punctuation">,</span> <span class="StringLit">"Queen"</span><span class="Punctuation">,</span> <span class="StringLit">"Jack"</span><span class="Punctuation">,</span> <span class="StringLit">"Ten"</span><span class="Punctuation">]</span>
- <span class="Identifier">shuffle</span><span class="Punctuation">(</span><span class="Identifier">cards</span><span class="Punctuation">)</span>
- <span class="Identifier">doAssert</span> <span class="Identifier">cards</span><span class="Operator">.</span><span class="Identifier">len</span> <span class="Operator">==</span> <span class="DecNumber">5</span></pre><p>These examples all use the default RNG. The <a class="reference external" href="#Rand">Rand type</a> represents the state of an RNG. For convenience, this module contains a default Rand state that corresponds to the default RNG. Most procs in this module which do not take in a Rand parameter, including those called in the above examples, use the default generator. Those procs are <strong>not</strong> thread-safe.</p>
- <p>Note that the default generator always starts in the same state. The <a class="reference external" href="#randomize">randomize proc</a> can be called to initialize the default generator with a seed based on the current time, and it only needs to be called once before the first usage of procs from this module. If <tt class="docutils literal"><span class="pre"><span class="Identifier">randomize</span></span></tt> is not called, the default generator will always produce the same results.</p>
- <p>RNGs that are independent of the default one can be created with the <a class="reference external" href="#initRand,int64">initRand proc</a>.</p>
- <p>Again, it is important to remember that this module must <strong>not</strong> be used for cryptographic applications.</p>
- <h1><a class="toc-backref" id="see-also" href="#see-also">See also</a></h1><ul class="simple"><li><a class="reference external" href="sysrand.html">std/sysrand module</a> for a cryptographically secure pseudorandom number generator</li>
- <li><a class="reference external" href="math.html">math module</a> for basic math routines</li>
- <li><a class="reference external" href="stats.html">stats module</a> for statistical analysis</li>
- <li><a class="reference external" href="lib.html#pure-libraries-hashing">list of cryptographic and hashing modules</a> in the standard library</li>
- </ul>
- </p>
- <div class="section" id="6">
- <h1><a class="toc-backref" href="#6">Imports</a></h1>
- <dl class="item">
- <a class="reference external" href="algorithm.html">algorithm</a>, <a class="reference external" href="math.html">math</a>, <a class="reference external" href="since.html">since</a>, <a class="reference external" href="jsutils.html">jsutils</a>, <a class="reference external" href="assertions.html">assertions</a>, <a class="reference external" href="hashes.html">hashes</a>, <a class="reference external" href="os.html">os</a>, <a class="reference external" href="sysrand.html">sysrand</a>, <a class="reference external" href="monotimes.html">monotimes</a>, <a class="reference external" href="locks.html">locks</a>
- </dl>
- </div>
- <div class="section" id="7">
- <h1><a class="toc-backref" href="#7">Types</a></h1>
- <dl class="item">
- <div id="Rand">
- <dt><pre><a href="random.html#Rand"><span class="Identifier">Rand</span></a> <span class="Other">=</span> <span class="Keyword">object</span></pre></dt>
- <dd>
-
- <p>State of a random number generator.</p>
- <p>Create a new Rand state using the <a class="reference external" href="#initRand,int64">initRand proc</a>.</p>
- <p>The module contains a default Rand state for convenience. It corresponds to the default RNG's state. The default Rand state always starts with the same values, but the <a class="reference external" href="#randomize">randomize proc</a> can be used to seed the default generator with a value based on the current time.</p>
- <p>Many procs have two variations: one that takes in a Rand parameter and another that uses the default generator. The procs that use the default generator are <strong>not</strong> thread-safe!</p>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L95" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L95" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </dl>
- </div>
- <div class="section" id="12">
- <h1><a class="toc-backref" href="#12">Procs</a></h1>
- <dl class="item">
- <div id="gauss-procs-all">
- <div id="gauss,float,float">
- <dt><pre><span class="Keyword">proc</span> <a href="#gauss%2Cfloat%2Cfloat"><span class="Identifier">gauss</span></a><span class="Other">(</span><span class="Identifier">mu</span> <span class="Other">=</span> <span class="FloatNumber">0.0</span><span class="Other">;</span> <span class="Identifier">sigma</span> <span class="Other">=</span> <span class="FloatNumber">1.0</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#float"><span class="Identifier">float</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- <p>Returns a Gaussian random variate, with mean <tt class="docutils literal"><span class="pre"><span class="Identifier">mu</span></span></tt> and standard deviation <tt class="docutils literal"><span class="pre"><span class="Identifier">sigma</span></span></tt>.</p>
- <p>If <a class="reference external" href="#randomize">randomize</a> has not been called, the order of outcomes from this proc will always be the same.</p>
- <p>This proc uses the default RNG. Thus, it is <strong>not</strong> thread-safe.</p>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L578" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L578" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- <div id="gauss,Rand,float,float">
- <dt><pre><span class="Keyword">proc</span> <a href="#gauss%2CRand%2Cfloat%2Cfloat"><span class="Identifier">gauss</span></a><span class="Other">(</span><span class="Identifier">r</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="random.html#Rand"><span class="Identifier">Rand</span></a><span class="Other">;</span> <span class="Identifier">mu</span> <span class="Other">=</span> <span class="FloatNumber">0.0</span><span class="Other">;</span> <span class="Identifier">sigma</span> <span class="Other">=</span> <span class="FloatNumber">1.0</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#float"><span class="Identifier">float</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span>
- <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- Returns a Gaussian random variate, with mean <tt class="docutils literal"><span class="pre"><span class="Identifier">mu</span></span></tt> and standard deviation <tt class="docutils literal"><span class="pre"><span class="Identifier">sigma</span></span></tt> using the given state.
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L562" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L562" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="initRand-procs-all">
- <div id="initRand">
- <dt><pre><span class="Keyword">proc</span> <a href="#initRand"><span class="Identifier">initRand</span></a><span class="Other">(</span><span class="Other">)</span><span class="Other">:</span> <a href="random.html#Rand"><span class="Identifier">Rand</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- <p>Initializes a new Rand state.</p>
- <p>The resulting state is independent of the default RNG's state.</p>
- <p><strong>Note:</strong> Does not work for the compile-time VM.</p>
- <p>See also:</p>
- <ul class="simple"><li><a class="reference external" href="#initRand,int64">initRand proc</a> that accepts a seed for a new Rand state</li>
- <li><a class="reference external" href="#randomize">randomize proc</a> that initializes the default RNG using the current time</li>
- <li><a class="reference external" href="#randomize,int64">randomize proc</a> that accepts a seed for the default RNG</li>
- </ul>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L687" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L687" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- <div id="initRand,int64">
- <dt><pre><span class="Keyword">proc</span> <a href="#initRand%2Cint64"><span class="Identifier">initRand</span></a><span class="Other">(</span><span class="Identifier">seed</span><span class="Other">:</span> <a href="system.html#int64"><span class="Identifier">int64</span></a><span class="Other">)</span><span class="Other">:</span> <a href="random.html#Rand"><span class="Identifier">Rand</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- <p>Initializes a new <a class="reference external" href="#Rand">Rand</a> state using the given seed.</p>
- <p>Providing a specific seed will produce the same results for that seed each time.</p>
- <p>The resulting state is independent of the default RNG's state. When <tt class="docutils literal"><span class="pre"><span class="Identifier">seed</span> <span class="Operator">==</span> <span class="DecNumber">0</span></span></tt>, we internally set the seed to an implementation defined non-zero value.</p>
- <p><strong>See also:</strong></p>
- <ul class="simple"><li><a class="reference external" href="#initRand">initRand proc</a> that uses the current time</li>
- <li><a class="reference external" href="#randomize,int64">randomize proc</a> that accepts a seed for the default RNG</li>
- <li><a class="reference external" href="#randomize">randomize proc</a> that initializes the default RNG using the current time</li>
- </ul>
- <p><strong class="examples_text">Example:</strong></p>
- <pre class="listing"><span class="Keyword">from</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">times</span> <span class="Keyword">import</span> <span class="Identifier">getTime</span><span class="Punctuation">,</span> <span class="Identifier">toUnix</span><span class="Punctuation">,</span> <span class="Identifier">nanosecond</span>
- <span class="Keyword">var</span> <span class="Identifier">r1</span> <span class="Operator">=</span> <span class="Identifier">initRand</span><span class="Punctuation">(</span><span class="DecNumber">123</span><span class="Punctuation">)</span>
- <span class="Keyword">let</span> <span class="Identifier">now</span> <span class="Operator">=</span> <span class="Identifier">getTime</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
- <span class="Keyword">var</span> <span class="Identifier">r2</span> <span class="Operator">=</span> <span class="Identifier">initRand</span><span class="Punctuation">(</span><span class="Identifier">now</span><span class="Operator">.</span><span class="Identifier">toUnix</span> <span class="Operator">*</span> <span class="DecNumber">1_000_000_000</span> <span class="Operator">+</span> <span class="Identifier">now</span><span class="Operator">.</span><span class="Identifier">nanosecond</span><span class="Punctuation">)</span></pre>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L588" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L588" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="next-procs-all">
- <div id="next,Rand">
- <dt><pre><span class="Keyword">proc</span> <a href="#next%2CRand"><span class="Identifier">next</span></a><span class="Other">(</span><span class="Identifier">r</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="random.html#Rand"><span class="Identifier">Rand</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#uint64"><span class="Identifier">uint64</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- <p>Computes a random <tt class="docutils literal"><span class="pre"><span class="Identifier">uint64</span></span></tt> number using the given state.</p>
- <p><strong>See also:</strong></p>
- <ul class="simple"><li><a class="reference external" href="#rand,Rand,Natural">rand proc</a> that returns an integer between zero and a given upper bound</li>
- <li><a class="reference external" href="#rand,Rand,range[]">rand proc</a> that returns a float</li>
- <li><a class="reference external" href="#rand,Rand,HSlice[T: Ordinal or float or float32 or float64,T: Ordinal or float or float32 or float64]">rand proc</a> that accepts a slice</li>
- <li><a class="reference external" href="#rand,typedesc[T]">rand proc</a> that accepts an integer or range type</li>
- <li><a class="reference external" href="#skipRandomNumbers,Rand">skipRandomNumbers proc</a></li>
- </ul>
- <p><strong class="examples_text">Example: cmd: -r:off</strong></p>
- <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">r</span> <span class="Operator">=</span> <span class="Identifier">initRand</span><span class="Punctuation">(</span><span class="DecNumber">2019</span><span class="Punctuation">)</span>
- <span class="Identifier">assert</span> <span class="Identifier">r</span><span class="Operator">.</span><span class="Identifier">next</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">13223559681708962501'</span><span class="Identifier">u64</span> <span class="Comment"># implementation defined</span>
- <span class="Identifier">assert</span> <span class="Identifier">r</span><span class="Operator">.</span><span class="Identifier">next</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">7229677234260823147'</span><span class="Identifier">u64</span> <span class="Comment"># ditto</span></pre>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L138" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L138" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="rand-procs-all">
- <div id="rand,float">
- <dt><pre><span class="Keyword">proc</span> <a href="#rand%2Cfloat"><span class="Identifier">rand</span></a><span class="Other">(</span><span class="Identifier">max</span><span class="Other">:</span> <a href="system.html#float"><span class="Identifier">float</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#float"><span class="Identifier">float</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">gcsafe</span><span class="Other">,</span> <span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- <p>Returns a random floating point number in the range <tt class="docutils literal"><span class="pre"><span class="FloatNumber">0.0</span><span class="Operator">..</span><span class="Identifier">max</span></span></tt>.</p>
- <p>If <a class="reference external" href="#randomize">randomize</a> has not been called, the sequence of random numbers returned from this proc will always be the same.</p>
- <p>This proc uses the default RNG. Thus, it is <strong>not</strong> thread-safe.</p>
- <p><strong>See also:</strong></p>
- <ul class="simple"><li><a class="reference external" href="#rand,Rand,range[]">rand proc</a> that returns a float using a provided state</li>
- <li><a class="reference external" href="#rand,int">rand proc</a> that returns an integer</li>
- <li><a class="reference external" href="#rand,HSlice[T: Ordinal or float or float32 or float64,T: Ordinal or float or float32 or float64]">rand proc</a> that accepts a slice</li>
- <li><a class="reference external" href="#rand,typedesc[T]">rand proc</a> that accepts an integer or range type</li>
- </ul>
- <p><strong class="examples_text">Example:</strong></p>
- <pre class="listing"><span class="Identifier">randomize</span><span class="Punctuation">(</span><span class="DecNumber">234</span><span class="Punctuation">)</span>
- <span class="Keyword">let</span> <span class="Identifier">f</span> <span class="Operator">=</span> <span class="Identifier">rand</span><span class="Punctuation">(</span><span class="FloatNumber">1.0</span><span class="Punctuation">)</span> <span class="Comment"># 8.717181376738381e-07</span></pre>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L311" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L311" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- <div id="rand,int">
- <dt><pre><span class="Keyword">proc</span> <a href="#rand%2Cint"><span class="Identifier">rand</span></a><span class="Other">(</span><span class="Identifier">max</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">gcsafe</span><span class="Other">,</span> <span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- <p>Returns a random integer in the range <tt class="docutils literal"><span class="pre"><span class="FloatNumber">0.</span><span class="Operator">.</span><span class="Identifier">max</span></span></tt>.</p>
- <p>If <a class="reference external" href="#randomize">randomize</a> has not been called, the sequence of random numbers returned from this proc will always be the same.</p>
- <p>This proc uses the default RNG. Thus, it is <strong>not</strong> thread-safe.</p>
- <p><strong>See also:</strong></p>
- <ul class="simple"><li><a class="reference external" href="#rand,Rand,Natural">rand proc</a> that returns an integer using a provided state</li>
- <li><a class="reference external" href="#rand,float">rand proc</a> that returns a float</li>
- <li><a class="reference external" href="#rand,HSlice[T: Ordinal or float or float32 or float64,T: Ordinal or float or float32 or float64]">rand proc</a> that accepts a slice</li>
- <li><a class="reference external" href="#rand,typedesc[T]">rand proc</a> that accepts an integer or range type</li>
- </ul>
- <p><strong class="examples_text">Example: cmd: -r:off</strong></p>
- <pre class="listing"><span class="Identifier">randomize</span><span class="Punctuation">(</span><span class="DecNumber">123</span><span class="Punctuation">)</span>
- <span class="Identifier">assert</span> <span class="Punctuation">[</span><span class="Identifier">rand</span><span class="Punctuation">(</span><span class="DecNumber">100</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Identifier">rand</span><span class="Punctuation">(</span><span class="DecNumber">100</span><span class="Punctuation">)</span><span class="Punctuation">]</span> <span class="Operator">==</span> <span class="Punctuation">[</span><span class="DecNumber">96</span><span class="Punctuation">,</span> <span class="DecNumber">63</span><span class="Punctuation">]</span> <span class="Comment"># implementation defined</span></pre>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L263" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L263" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- <div id="rand,Rand,Natural">
- <dt><pre><span class="Keyword">proc</span> <a href="#rand%2CRand%2CNatural"><span class="Identifier">rand</span></a><span class="Other">(</span><span class="Identifier">r</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="random.html#Rand"><span class="Identifier">Rand</span></a><span class="Other">;</span> <span class="Identifier">max</span><span class="Other">:</span> <a href="system.html#Natural"><span class="Identifier">Natural</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">gcsafe</span><span class="Other">,</span> <span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span>
- <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- <p>Returns a random integer in the range <tt class="docutils literal"><span class="pre"><span class="FloatNumber">0.</span><span class="Operator">.</span><span class="Identifier">max</span></span></tt> using the given state.</p>
- <p><strong>See also:</strong></p>
- <ul class="simple"><li><a class="reference external" href="#rand,int">rand proc</a> that returns an integer using the default RNG</li>
- <li><a class="reference external" href="#rand,Rand,range[]">rand proc</a> that returns a float</li>
- <li><a class="reference external" href="#rand,Rand,HSlice[T: Ordinal or float or float32 or float64,T: Ordinal or float or float32 or float64]">rand proc</a> that accepts a slice</li>
- <li><a class="reference external" href="#rand,typedesc[T]">rand proc</a> that accepts an integer or range type</li>
- </ul>
- <p><strong class="examples_text">Example:</strong></p>
- <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">r</span> <span class="Operator">=</span> <span class="Identifier">initRand</span><span class="Punctuation">(</span><span class="DecNumber">123</span><span class="Punctuation">)</span>
- <span class="Keyword">if</span> <span class="Identifier">false</span><span class="Punctuation">:</span>
- <span class="Identifier">assert</span> <span class="Identifier">r</span><span class="Operator">.</span><span class="Identifier">rand</span><span class="Punctuation">(</span><span class="DecNumber">100</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">96</span> <span class="Comment"># implementation defined</span></pre>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L246" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L246" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- <div id="rand,Rand,range[]">
- <dt><pre><span class="Keyword">proc</span> <a href="#rand%2CRand%2Crange%5B%5D"><span class="Identifier">rand</span></a><span class="Other">(</span><span class="Identifier">r</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="random.html#Rand"><span class="Identifier">Rand</span></a><span class="Other">;</span> <span class="Identifier">max</span><span class="Other">:</span> <a href="system.html#range"><span class="Identifier">range</span></a><span class="Other">[</span><span class="FloatNumber">0.0</span> <span class="Operator">..</span> <span class="Identifier">high</span><span class="Other">(</span><a href="system.html#float"><span class="Identifier">float</span></a><span class="Other">)</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#float"><span class="Identifier">float</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">gcsafe</span><span class="Other">,</span>
- <span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- <p>Returns a random floating point number in the range <tt class="docutils literal"><span class="pre"><span class="FloatNumber">0.0</span><span class="Operator">..</span><span class="Identifier">max</span></span></tt> using the given state.</p>
- <p><strong>See also:</strong></p>
- <ul class="simple"><li><a class="reference external" href="#rand,float">rand proc</a> that returns a float using the default RNG</li>
- <li><a class="reference external" href="#rand,Rand,Natural">rand proc</a> that returns an integer</li>
- <li><a class="reference external" href="#rand,Rand,HSlice[T: Ordinal or float or float32 or float64,T: Ordinal or float or float32 or float64]">rand proc</a> that accepts a slice</li>
- <li><a class="reference external" href="#rand,typedesc[T]">rand proc</a> that accepts an integer or range type</li>
- </ul>
- <p><strong class="examples_text">Example:</strong></p>
- <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">r</span> <span class="Operator">=</span> <span class="Identifier">initRand</span><span class="Punctuation">(</span><span class="DecNumber">234</span><span class="Punctuation">)</span>
- <span class="Keyword">let</span> <span class="Identifier">f</span> <span class="Operator">=</span> <span class="Identifier">r</span><span class="Operator">.</span><span class="Identifier">rand</span><span class="Punctuation">(</span><span class="FloatNumber">1.0</span><span class="Punctuation">)</span> <span class="Comment"># 8.717181376738381e-07</span></pre>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L284" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L284" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- <div id="rand,Rand,HSlice[T: Ordinal or float or float32 or float64,T: Ordinal or float or float32 or float64]">
- <dt><pre><span class="Keyword">proc</span> <a href="#rand%2CRand%2CHSlice%5BT%3A%20Ordinal%20or%20float%20or%20float32%20or%20float64%2CT%3A%20Ordinal%20or%20float%20or%20float32%20or%20float64%5D"><span class="Identifier">rand</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#Ordinal"><span class="Identifier">Ordinal</span></a> <span class="Keyword">or</span> <a href="system.html#SomeFloat"><span class="Identifier">SomeFloat</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">r</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="random.html#Rand"><span class="Identifier">Rand</span></a><span class="Other">;</span> <span class="Identifier">x</span><span class="Other">:</span> <a href="system.html#HSlice"><span class="Identifier">HSlice</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">,</span> <span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
- <dd>
-
- <p>For a slice <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span><span class="Operator">..</span><span class="Identifier">b</span></span></tt>, returns a value in the range <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span><span class="Operator">..</span><span class="Identifier">b</span></span></tt> using the given state.</p>
- <p>Allowed types for <tt class="docutils literal"><span class="pre"><span class="Identifier">T</span></span></tt> are integers, floats, and enums without holes.</p>
- <p><strong>See also:</strong></p>
- <ul class="simple"><li><a class="reference external" href="#rand,HSlice[T: Ordinal or float or float32 or float64,T: Ordinal or float or float32 or float64]">rand proc</a> that accepts a slice and uses the default RNG</li>
- <li><a class="reference external" href="#rand,Rand,Natural">rand proc</a> that returns an integer</li>
- <li><a class="reference external" href="#rand,Rand,range[]">rand proc</a> that returns a float</li>
- <li><a class="reference external" href="#rand,typedesc[T]">rand proc</a> that accepts an integer or range type</li>
- </ul>
- <p><strong class="examples_text">Example:</strong></p>
- <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">r</span> <span class="Operator">=</span> <span class="Identifier">initRand</span><span class="Punctuation">(</span><span class="DecNumber">345</span><span class="Punctuation">)</span>
- <span class="Identifier">assert</span> <span class="Identifier">r</span><span class="Operator">.</span><span class="Identifier">rand</span><span class="Punctuation">(</span><span class="FloatNumber">1.</span><span class="Operator">.</span><span class="DecNumber">5</span><span class="Punctuation">)</span> <span class="Operator"><=</span> <span class="DecNumber">5</span>
- <span class="Identifier">assert</span> <span class="Identifier">r</span><span class="Operator">.</span><span class="Identifier">rand</span><span class="Punctuation">(</span><span class="Operator">-</span><span class="FloatNumber">1.1</span> <span class="Operator">..</span> <span class="FloatNumber">1.2</span><span class="Punctuation">)</span> <span class="Operator">>=</span> <span class="Operator">-</span><span class="FloatNumber">1.1</span></pre>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L332" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L332" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- <div id="rand,HSlice[T: Ordinal or float or float32 or float64,T: Ordinal or float or float32 or float64]">
- <dt><pre><span class="Keyword">proc</span> <a href="#rand%2CHSlice%5BT%3A%20Ordinal%20or%20float%20or%20float32%20or%20float64%2CT%3A%20Ordinal%20or%20float%20or%20float32%20or%20float64%5D"><span class="Identifier">rand</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#Ordinal"><span class="Identifier">Ordinal</span></a> <span class="Keyword">or</span> <a href="system.html#SomeFloat"><span class="Identifier">SomeFloat</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="system.html#HSlice"><span class="Identifier">HSlice</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">,</span> <span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
- <dd>
-
- <p>For a slice <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span><span class="Operator">..</span><span class="Identifier">b</span></span></tt>, returns a value in the range <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span><span class="Operator">..</span><span class="Identifier">b</span></span></tt>.</p>
- <p>Allowed types for <tt class="docutils literal"><span class="pre"><span class="Identifier">T</span></span></tt> are integers, floats, and enums without holes.</p>
- <p>If <a class="reference external" href="#randomize">randomize</a> has not been called, the sequence of random numbers returned from this proc will always be the same.</p>
- <p>This proc uses the default RNG. Thus, it is <strong>not</strong> thread-safe.</p>
- <p><strong>See also:</strong></p>
- <ul class="simple"><li><a class="reference external" href="#rand,Rand,HSlice[T: Ordinal or float or float32 or float64,T: Ordinal or float or float32 or float64]">rand proc</a> that accepts a slice and uses a provided state</li>
- <li><a class="reference external" href="#rand,int">rand proc</a> that returns an integer</li>
- <li><a class="reference external" href="#rand,float">rand proc</a> that returns a floating point number</li>
- <li><a class="reference external" href="#rand,typedesc[T]">rand proc</a> that accepts an integer or range type</li>
- </ul>
- <p><strong class="examples_text">Example:</strong></p>
- <pre class="listing"><span class="Identifier">randomize</span><span class="Punctuation">(</span><span class="DecNumber">345</span><span class="Punctuation">)</span>
- <span class="Identifier">assert</span> <span class="Identifier">rand</span><span class="Punctuation">(</span><span class="FloatNumber">1.</span><span class="Operator">.</span><span class="DecNumber">6</span><span class="Punctuation">)</span> <span class="Operator"><=</span> <span class="DecNumber">6</span></pre>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L357" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L357" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- <div id="rand,Rand,typedesc[T]">
- <dt><pre><span class="Keyword">proc</span> <a href="#rand%2CRand%2Ctypedesc%5BT%5D"><span class="Identifier">rand</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#Ordinal"><span class="Identifier">Ordinal</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">r</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="random.html#Rand"><span class="Identifier">Rand</span></a><span class="Other">;</span> <span class="Identifier">t</span><span class="Other">:</span> <a href="system.html#typedesc"><span class="Identifier">typedesc</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
- <dd>
-
- <p>Returns a random Ordinal in the range <tt class="docutils literal"><span class="pre"><span class="Identifier">low</span><span class="Punctuation">(</span><span class="Identifier">T</span><span class="Punctuation">)</span><span class="Operator">..</span><span class="Identifier">high</span><span class="Punctuation">(</span><span class="Identifier">T</span><span class="Punctuation">)</span></span></tt>.</p>
- <p>If <a class="reference external" href="#randomize">randomize</a> has not been called, the sequence of random numbers returned from this proc will always be the same.</p>
- <p><strong>See also:</strong></p>
- <ul class="simple"><li><a class="reference external" href="#rand,int">rand proc</a> that returns an integer</li>
- <li><a class="reference external" href="#rand,float">rand proc</a> that returns a floating point number</li>
- <li><a class="reference external" href="#rand,HSlice[T: Ordinal or float or float32 or float64,T: Ordinal or float or float32 or float64]">rand proc</a> that accepts a slice</li>
- </ul>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L379" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L379" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- <div id="rand,typedesc[T]">
- <dt><pre><span class="Keyword">proc</span> <a href="#rand%2Ctypedesc%5BT%5D"><span class="Identifier">rand</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#Ordinal"><span class="Identifier">Ordinal</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">t</span><span class="Other">:</span> <a href="system.html#typedesc"><span class="Identifier">typedesc</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
- <dd>
-
- <p>Returns a random Ordinal in the range <tt class="docutils literal"><span class="pre"><span class="Identifier">low</span><span class="Punctuation">(</span><span class="Identifier">T</span><span class="Punctuation">)</span><span class="Operator">..</span><span class="Identifier">high</span><span class="Punctuation">(</span><span class="Identifier">T</span><span class="Punctuation">)</span></span></tt>.</p>
- <p>If <a class="reference external" href="#randomize">randomize</a> has not been called, the sequence of random numbers returned from this proc will always be the same.</p>
- <p>This proc uses the default RNG. Thus, it is <strong>not</strong> thread-safe.</p>
- <p><strong>See also:</strong></p>
- <ul class="simple"><li><a class="reference external" href="#rand,int">rand proc</a> that returns an integer</li>
- <li><a class="reference external" href="#rand,float">rand proc</a> that returns a floating point number</li>
- <li><a class="reference external" href="#rand,HSlice[T: Ordinal or float or float32 or float64,T: Ordinal or float or float32 or float64]">rand proc</a> that accepts a slice</li>
- </ul>
- <p><strong class="examples_text">Example:</strong></p>
- <pre class="listing"><span class="Identifier">randomize</span><span class="Punctuation">(</span><span class="DecNumber">567</span><span class="Punctuation">)</span>
- <span class="Keyword">type</span> <span class="Identifier">E</span> <span class="Operator">=</span> <span class="Keyword">enum</span> <span class="Identifier">a</span><span class="Punctuation">,</span> <span class="Identifier">b</span><span class="Punctuation">,</span> <span class="Identifier">c</span><span class="Punctuation">,</span> <span class="Identifier">d</span>
- <span class="Identifier">assert</span> <span class="Identifier">rand</span><span class="Punctuation">(</span><span class="Identifier">E</span><span class="Punctuation">)</span> <span class="Keyword">in</span> <span class="Identifier">a</span><span class="Operator">..</span><span class="Identifier">d</span>
- <span class="Identifier">assert</span> <span class="Identifier">rand</span><span class="Punctuation">(</span><span class="Identifier">char</span><span class="Punctuation">)</span> <span class="Keyword">in</span> <span class="Identifier">low</span><span class="Punctuation">(</span><span class="Identifier">char</span><span class="Punctuation">)</span><span class="Operator">..</span><span class="Identifier">high</span><span class="Punctuation">(</span><span class="Identifier">char</span><span class="Punctuation">)</span>
- <span class="Identifier">assert</span> <span class="Identifier">rand</span><span class="Punctuation">(</span><span class="Identifier">int8</span><span class="Punctuation">)</span> <span class="Keyword">in</span> <span class="Identifier">low</span><span class="Punctuation">(</span><span class="Identifier">int8</span><span class="Punctuation">)</span><span class="Operator">..</span><span class="Identifier">high</span><span class="Punctuation">(</span><span class="Identifier">int8</span><span class="Punctuation">)</span>
- <span class="Identifier">assert</span> <span class="Identifier">rand</span><span class="Punctuation">(</span><span class="Identifier">uint32</span><span class="Punctuation">)</span> <span class="Keyword">in</span> <span class="Identifier">low</span><span class="Punctuation">(</span><span class="Identifier">uint32</span><span class="Punctuation">)</span><span class="Operator">..</span><span class="Identifier">high</span><span class="Punctuation">(</span><span class="Identifier">uint32</span><span class="Punctuation">)</span>
- <span class="Identifier">assert</span> <span class="Identifier">rand</span><span class="Punctuation">(</span><span class="Identifier">range</span><span class="Punctuation">[</span><span class="FloatNumber">1.</span><span class="Operator">.</span><span class="DecNumber">16</span><span class="Punctuation">]</span><span class="Punctuation">)</span> <span class="Keyword">in</span> <span class="FloatNumber">1.</span><span class="Operator">.</span><span class="DecNumber">16</span></pre>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L400" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L400" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="randomize-procs-all">
- <div id="randomize">
- <dt><pre><span class="Keyword">proc</span> <a href="#randomize"><span class="Identifier">randomize</span></a><span class="Other">(</span><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">gcsafe</span><span class="Other">,</span> <span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- <p>Initializes the default random number generator with a seed based on random number source.</p>
- <p>This proc only needs to be called once, and it should be called before the first usage of procs from this module that use the default RNG.</p>
- <p><strong>Note:</strong> Does not work for the compile-time VM.</p>
- <p><strong>See also:</strong></p>
- <ul class="simple"><li><a class="reference external" href="#randomize,int64">randomize proc</a> that accepts a seed</li>
- <li><a class="reference external" href="#initRand">initRand proc</a> that initializes a Rand state using the current time</li>
- <li><a class="reference external" href="#initRand,int64">initRand proc</a> that initializes a Rand state with a given seed</li>
- </ul>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L739" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L739" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- <div id="randomize,int64">
- <dt><pre><span class="Keyword">proc</span> <a href="#randomize%2Cint64"><span class="Identifier">randomize</span></a><span class="Other">(</span><span class="Identifier">seed</span><span class="Other">:</span> <a href="system.html#int64"><span class="Identifier">int64</span></a><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">gcsafe</span><span class="Other">,</span> <span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- <p>Initializes the default random number generator with the given seed.</p>
- <p>Providing a specific seed will produce the same results for that seed each time.</p>
- <p><strong>See also:</strong></p>
- <ul class="simple"><li><a class="reference external" href="#initRand,int64">initRand proc</a> that initializes a Rand state with a given seed</li>
- <li><a class="reference external" href="#randomize">randomize proc</a> that uses the current time instead</li>
- <li><a class="reference external" href="#initRand">initRand proc</a> that initializes a Rand state using the current time</li>
- </ul>
- <p><strong class="examples_text">Example:</strong></p>
- <pre class="listing"><span class="Keyword">from</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">times</span> <span class="Keyword">import</span> <span class="Identifier">getTime</span><span class="Punctuation">,</span> <span class="Identifier">toUnix</span><span class="Punctuation">,</span> <span class="Identifier">nanosecond</span>
- <span class="Identifier">randomize</span><span class="Punctuation">(</span><span class="DecNumber">123</span><span class="Punctuation">)</span>
- <span class="Keyword">let</span> <span class="Identifier">now</span> <span class="Operator">=</span> <span class="Identifier">getTime</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
- <span class="Identifier">randomize</span><span class="Punctuation">(</span><span class="Identifier">now</span><span class="Operator">.</span><span class="Identifier">toUnix</span> <span class="Operator">*</span> <span class="DecNumber">1_000_000_000</span> <span class="Operator">+</span> <span class="Identifier">now</span><span class="Operator">.</span><span class="Identifier">nanosecond</span><span class="Punctuation">)</span></pre>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L615" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L615" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="sample-procs-all">
- <div id="sample,openArray[T],openArray[U]">
- <dt><pre><span class="Keyword">proc</span> <a href="#sample%2CopenArray%5BT%5D%2CopenArray%5BU%5D"><span class="Identifier">sample</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">,</span> <span class="Identifier">U</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">a</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">cdf</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">U</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
- <dd>
-
- <p>Returns an element from <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span></span></tt> using a cumulative distribution function (CDF).</p>
- <p>This proc works similarly to <a class="reference external" href="#sample,Rand,openArray[T],openArray[U]">sample</a>. See that proc's documentation for more details.</p>
- <p>If <a class="reference external" href="#randomize">randomize</a> has not been called, the order of outcomes from this proc will always be the same.</p>
- <p>This proc uses the default RNG. Thus, it is <strong>not</strong> thread-safe.</p>
- <p><strong>See also:</strong></p>
- <ul class="simple"><li><a class="reference external" href="#sample,Rand,openArray[T],openArray[U]">sample proc</a> that also utilizes a CDF but uses a provided state</li>
- <li><a class="reference external" href="#sample,openArray[T]">sample proc</a> that does not use a CDF</li>
- <li><a class="reference external" href="#sample,set[T]">sample proc</a> for sets</li>
- </ul>
- <p><strong class="examples_text">Example:</strong></p>
- <pre class="listing"><span class="Keyword">from</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">math</span> <span class="Keyword">import</span> <span class="Identifier">cumsummed</span>
- <span class="Keyword">let</span> <span class="Identifier">marbles</span> <span class="Operator">=</span> <span class="Punctuation">[</span><span class="StringLit">"red"</span><span class="Punctuation">,</span> <span class="StringLit">"blue"</span><span class="Punctuation">,</span> <span class="StringLit">"green"</span><span class="Punctuation">,</span> <span class="StringLit">"yellow"</span><span class="Punctuation">,</span> <span class="StringLit">"purple"</span><span class="Punctuation">]</span>
- <span class="Keyword">let</span> <span class="Identifier">count</span> <span class="Operator">=</span> <span class="Punctuation">[</span><span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">6</span><span class="Punctuation">,</span> <span class="DecNumber">8</span><span class="Punctuation">,</span> <span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="DecNumber">4</span><span class="Punctuation">]</span>
- <span class="Keyword">let</span> <span class="Identifier">cdf</span> <span class="Operator">=</span> <span class="Identifier">count</span><span class="Operator">.</span><span class="Identifier">cumsummed</span>
- <span class="Identifier">randomize</span><span class="Punctuation">(</span><span class="DecNumber">789</span><span class="Punctuation">)</span>
- <span class="Identifier">assert</span> <span class="Identifier">sample</span><span class="Punctuation">(</span><span class="Identifier">marbles</span><span class="Punctuation">,</span> <span class="Identifier">cdf</span><span class="Punctuation">)</span> <span class="Keyword">in</span> <span class="Identifier">marbles</span></pre>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L533" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L533" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- <div id="sample,Rand,openArray[T],openArray[U]">
- <dt><pre><span class="Keyword">proc</span> <a href="#sample%2CRand%2CopenArray%5BT%5D%2CopenArray%5BU%5D"><span class="Identifier">sample</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">,</span> <span class="Identifier">U</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">r</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="random.html#Rand"><span class="Identifier">Rand</span></a><span class="Other">;</span> <span class="Identifier">a</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">cdf</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">U</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
- <dd>
-
- <p>Returns an element from <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span></span></tt> using a cumulative distribution function (CDF) and the given state.</p>
- <p>The <tt class="docutils literal"><span class="pre"><span class="Identifier">cdf</span></span></tt> argument does not have to be normalized, and it could contain any type of elements that can be converted to a <tt class="docutils literal"><span class="pre"><span class="Identifier">float</span></span></tt>. It must be the same length as <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span></span></tt>. Each element in <tt class="docutils literal"><span class="pre"><span class="Identifier">cdf</span></span></tt> should be greater than or equal to the previous element.</p>
- <p>The outcome of the <a class="reference external" href="math.html#cumsum,openArray[T]">cumsum</a> proc and the return value of the <a class="reference external" href="math.html#cumsummed,openArray[T]">cumsummed</a> proc, which are both in the math module, can be used as the <tt class="docutils literal"><span class="pre"><span class="Identifier">cdf</span></span></tt> argument.</p>
- <p><strong>See also:</strong></p>
- <ul class="simple"><li><a class="reference external" href="#sample,openArray[T],openArray[U]">sample proc</a> that also utilizes a CDF but uses the default RNG</li>
- <li><a class="reference external" href="#sample,Rand,openArray[T]">sample proc</a> that does not use a CDF</li>
- <li><a class="reference external" href="#sample,Rand,set[T]">sample proc</a> for sets</li>
- </ul>
- <p><strong class="examples_text">Example:</strong></p>
- <pre class="listing"><span class="Keyword">from</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">math</span> <span class="Keyword">import</span> <span class="Identifier">cumsummed</span>
- <span class="Keyword">let</span> <span class="Identifier">marbles</span> <span class="Operator">=</span> <span class="Punctuation">[</span><span class="StringLit">"red"</span><span class="Punctuation">,</span> <span class="StringLit">"blue"</span><span class="Punctuation">,</span> <span class="StringLit">"green"</span><span class="Punctuation">,</span> <span class="StringLit">"yellow"</span><span class="Punctuation">,</span> <span class="StringLit">"purple"</span><span class="Punctuation">]</span>
- <span class="Keyword">let</span> <span class="Identifier">count</span> <span class="Operator">=</span> <span class="Punctuation">[</span><span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">6</span><span class="Punctuation">,</span> <span class="DecNumber">8</span><span class="Punctuation">,</span> <span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="DecNumber">4</span><span class="Punctuation">]</span>
- <span class="Keyword">let</span> <span class="Identifier">cdf</span> <span class="Operator">=</span> <span class="Identifier">count</span><span class="Operator">.</span><span class="Identifier">cumsummed</span>
- <span class="Keyword">var</span> <span class="Identifier">r</span> <span class="Operator">=</span> <span class="Identifier">initRand</span><span class="Punctuation">(</span><span class="DecNumber">789</span><span class="Punctuation">)</span>
- <span class="Identifier">assert</span> <span class="Identifier">r</span><span class="Operator">.</span><span class="Identifier">sample</span><span class="Punctuation">(</span><span class="Identifier">marbles</span><span class="Punctuation">,</span> <span class="Identifier">cdf</span><span class="Punctuation">)</span> <span class="Keyword">in</span> <span class="Identifier">marbles</span></pre>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L499" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L499" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- <div id="sample,openArray[T]">
- <dt><pre><span class="Keyword">proc</span> <a href="#sample%2CopenArray%5BT%5D"><span class="Identifier">sample</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">a</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#lent"><span class="Identifier">lent</span></a> <span class="Identifier">T</span></pre></dt>
- <dd>
-
- <p>Returns a random element from <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span></span></tt>.</p>
- <p>If <a class="reference external" href="#randomize">randomize</a> has not been called, the order of outcomes from this proc will always be the same.</p>
- <p>This proc uses the default RNG. Thus, it is <strong>not</strong> thread-safe.</p>
- <p><strong>See also:</strong></p>
- <ul class="simple"><li><a class="reference external" href="#sample,Rand,openArray[T]">sample proc</a> that uses a provided state</li>
- <li><a class="reference external" href="#sample,openArray[T],openArray[U]">sample proc</a> that uses a cumulative distribution function</li>
- <li><a class="reference external" href="#sample,set[T]">sample proc</a> for sets</li>
- </ul>
- <p><strong class="examples_text">Example:</strong></p>
- <pre class="listing"><span class="Keyword">let</span> <span class="Identifier">marbles</span> <span class="Operator">=</span> <span class="Punctuation">[</span><span class="StringLit">"red"</span><span class="Punctuation">,</span> <span class="StringLit">"blue"</span><span class="Punctuation">,</span> <span class="StringLit">"green"</span><span class="Punctuation">,</span> <span class="StringLit">"yellow"</span><span class="Punctuation">,</span> <span class="StringLit">"purple"</span><span class="Punctuation">]</span>
- <span class="Identifier">randomize</span><span class="Punctuation">(</span><span class="DecNumber">456</span><span class="Punctuation">)</span>
- <span class="Identifier">assert</span> <span class="Identifier">sample</span><span class="Punctuation">(</span><span class="Identifier">marbles</span><span class="Punctuation">)</span> <span class="Keyword">in</span> <span class="Identifier">marbles</span></pre>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L479" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L479" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- <div id="sample,Rand,openArray[T]">
- <dt><pre><span class="Keyword">proc</span> <a href="#sample%2CRand%2CopenArray%5BT%5D"><span class="Identifier">sample</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">r</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="random.html#Rand"><span class="Identifier">Rand</span></a><span class="Other">;</span> <span class="Identifier">a</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
- <dd>
-
- <p>Returns a random element from <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span></span></tt> using the given state.</p>
- <p><strong>See also:</strong></p>
- <ul class="simple"><li><a class="reference external" href="#sample,openArray[T]">sample proc</a> that uses the default RNG</li>
- <li><a class="reference external" href="#sample,Rand,openArray[T],openArray[U]">sample proc</a> that uses a cumulative distribution function</li>
- <li><a class="reference external" href="#sample,Rand,set[T]">sample proc</a> for sets</li>
- </ul>
- <p><strong class="examples_text">Example:</strong></p>
- <pre class="listing"><span class="Keyword">let</span> <span class="Identifier">marbles</span> <span class="Operator">=</span> <span class="Punctuation">[</span><span class="StringLit">"red"</span><span class="Punctuation">,</span> <span class="StringLit">"blue"</span><span class="Punctuation">,</span> <span class="StringLit">"green"</span><span class="Punctuation">,</span> <span class="StringLit">"yellow"</span><span class="Punctuation">,</span> <span class="StringLit">"purple"</span><span class="Punctuation">]</span>
- <span class="Keyword">var</span> <span class="Identifier">r</span> <span class="Operator">=</span> <span class="Identifier">initRand</span><span class="Punctuation">(</span><span class="DecNumber">456</span><span class="Punctuation">)</span>
- <span class="Identifier">assert</span> <span class="Identifier">r</span><span class="Operator">.</span><span class="Identifier">sample</span><span class="Punctuation">(</span><span class="Identifier">marbles</span><span class="Punctuation">)</span> <span class="Keyword">in</span> <span class="Identifier">marbles</span></pre>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L464" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L464" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- <div id="sample,Rand,set[T]">
- <dt><pre><span class="Keyword">proc</span> <a href="#sample%2CRand%2Cset%5BT%5D"><span class="Identifier">sample</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">r</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="random.html#Rand"><span class="Identifier">Rand</span></a><span class="Other">;</span> <span class="Identifier">s</span><span class="Other">:</span> <a href="system.html#set"><span class="Identifier">set</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
- <dd>
-
- <p>Returns a random element from the set <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt> using the given state.</p>
- <p><strong>See also:</strong></p>
- <ul class="simple"><li><a class="reference external" href="#sample,set[T]">sample proc</a> that uses the default RNG</li>
- <li><a class="reference external" href="#sample,Rand,openArray[T]">sample proc</a> for <tt class="docutils literal"><span class="pre"><span class="Identifier">openArray</span></span></tt>s</li>
- <li><a class="reference external" href="#sample,Rand,openArray[T],openArray[U]">sample proc</a> that uses a cumulative distribution function</li>
- </ul>
- <p><strong class="examples_text">Example:</strong></p>
- <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">r</span> <span class="Operator">=</span> <span class="Identifier">initRand</span><span class="Punctuation">(</span><span class="DecNumber">987</span><span class="Punctuation">)</span>
- <span class="Keyword">let</span> <span class="Identifier">s</span> <span class="Operator">=</span> <span class="Punctuation">{</span><span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="DecNumber">7</span><span class="Punctuation">,</span> <span class="DecNumber">9</span><span class="Punctuation">}</span>
- <span class="Identifier">assert</span> <span class="Identifier">r</span><span class="Operator">.</span><span class="Identifier">sample</span><span class="Punctuation">(</span><span class="Identifier">s</span><span class="Punctuation">)</span> <span class="Keyword">in</span> <span class="Identifier">s</span></pre>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L425" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L425" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- <div id="sample,set[T]">
- <dt><pre><span class="Keyword">proc</span> <a href="#sample%2Cset%5BT%5D"><span class="Identifier">sample</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <a href="system.html#set"><span class="Identifier">set</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
- <dd>
-
- <p>Returns a random element from the set <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt>.</p>
- <p>If <a class="reference external" href="#randomize">randomize</a> has not been called, the order of outcomes from this proc will always be the same.</p>
- <p>This proc uses the default RNG. Thus, it is <strong>not</strong> thread-safe.</p>
- <p><strong>See also:</strong></p>
- <ul class="simple"><li><a class="reference external" href="#sample,Rand,set[T]">sample proc</a> that uses a provided state</li>
- <li><a class="reference external" href="#sample,openArray[T]">sample proc</a> for <tt class="docutils literal"><span class="pre"><span class="Identifier">openArray</span></span></tt>s</li>
- <li><a class="reference external" href="#sample,openArray[T],openArray[U]">sample proc</a> that uses a cumulative distribution function</li>
- </ul>
- <p><strong class="examples_text">Example:</strong></p>
- <pre class="listing"><span class="Identifier">randomize</span><span class="Punctuation">(</span><span class="DecNumber">987</span><span class="Punctuation">)</span>
- <span class="Keyword">let</span> <span class="Identifier">s</span> <span class="Operator">=</span> <span class="Punctuation">{</span><span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="DecNumber">7</span><span class="Punctuation">,</span> <span class="DecNumber">9</span><span class="Punctuation">}</span>
- <span class="Identifier">assert</span> <span class="Identifier">sample</span><span class="Punctuation">(</span><span class="Identifier">s</span><span class="Punctuation">)</span> <span class="Keyword">in</span> <span class="Identifier">s</span></pre>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L444" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L444" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="shuffle-procs-all">
- <div id="shuffle,Rand,openArray[T]">
- <dt><pre><span class="Keyword">proc</span> <a href="#shuffle%2CRand%2CopenArray%5BT%5D"><span class="Identifier">shuffle</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">r</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="random.html#Rand"><span class="Identifier">Rand</span></a><span class="Other">;</span> <span class="Identifier">x</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span></pre></dt>
- <dd>
-
- <p>Shuffles a sequence of elements in-place using the given state.</p>
- <p><strong>See also:</strong></p>
- <ul class="simple"><li><a class="reference external" href="#shuffle,openArray[T]">shuffle proc</a> that uses the default RNG</li>
- </ul>
- <p><strong class="examples_text">Example:</strong></p>
- <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">cards</span> <span class="Operator">=</span> <span class="Punctuation">[</span><span class="StringLit">"Ace"</span><span class="Punctuation">,</span> <span class="StringLit">"King"</span><span class="Punctuation">,</span> <span class="StringLit">"Queen"</span><span class="Punctuation">,</span> <span class="StringLit">"Jack"</span><span class="Punctuation">,</span> <span class="StringLit">"Ten"</span><span class="Punctuation">]</span>
- <span class="Keyword">var</span> <span class="Identifier">r</span> <span class="Operator">=</span> <span class="Identifier">initRand</span><span class="Punctuation">(</span><span class="DecNumber">678</span><span class="Punctuation">)</span>
- <span class="Identifier">r</span><span class="Operator">.</span><span class="Identifier">shuffle</span><span class="Punctuation">(</span><span class="Identifier">cards</span><span class="Punctuation">)</span>
- <span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">algorithm</span>
- <span class="Identifier">assert</span> <span class="Identifier">cards</span><span class="Operator">.</span><span class="Identifier">sorted</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">"Ace"</span><span class="Punctuation">,</span> <span class="StringLit">"Jack"</span><span class="Punctuation">,</span> <span class="StringLit">"King"</span><span class="Punctuation">,</span> <span class="StringLit">"Queen"</span><span class="Punctuation">,</span> <span class="StringLit">"Ten"</span><span class="Punctuation">]</span></pre>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L636" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L636" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- <div id="shuffle,openArray[T]">
- <dt><pre><span class="Keyword">proc</span> <a href="#shuffle%2CopenArray%5BT%5D"><span class="Identifier">shuffle</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span></pre></dt>
- <dd>
-
- <p>Shuffles a sequence of elements in-place.</p>
- <p>If <a class="reference external" href="#randomize">randomize</a> has not been called, the order of outcomes from this proc will always be the same.</p>
- <p>This proc uses the default RNG. Thus, it is <strong>not</strong> thread-safe.</p>
- <p><strong>See also:</strong></p>
- <ul class="simple"><li><a class="reference external" href="#shuffle,Rand,openArray[T]">shuffle proc</a> that uses a provided state</li>
- </ul>
- <p><strong class="examples_text">Example:</strong></p>
- <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">cards</span> <span class="Operator">=</span> <span class="Punctuation">[</span><span class="StringLit">"Ace"</span><span class="Punctuation">,</span> <span class="StringLit">"King"</span><span class="Punctuation">,</span> <span class="StringLit">"Queen"</span><span class="Punctuation">,</span> <span class="StringLit">"Jack"</span><span class="Punctuation">,</span> <span class="StringLit">"Ten"</span><span class="Punctuation">]</span>
- <span class="Identifier">randomize</span><span class="Punctuation">(</span><span class="DecNumber">678</span><span class="Punctuation">)</span>
- <span class="Identifier">shuffle</span><span class="Punctuation">(</span><span class="Identifier">cards</span><span class="Punctuation">)</span>
- <span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">algorithm</span>
- <span class="Identifier">assert</span> <span class="Identifier">cards</span><span class="Operator">.</span><span class="Identifier">sorted</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">"Ace"</span><span class="Punctuation">,</span> <span class="StringLit">"Jack"</span><span class="Punctuation">,</span> <span class="StringLit">"King"</span><span class="Punctuation">,</span> <span class="StringLit">"Queen"</span><span class="Punctuation">,</span> <span class="StringLit">"Ten"</span><span class="Punctuation">]</span></pre>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L652" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L652" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="skipRandomNumbers-procs-all">
- <div id="skipRandomNumbers,Rand">
- <dt><pre><span class="Keyword">proc</span> <a href="#skipRandomNumbers%2CRand"><span class="Identifier">skipRandomNumbers</span></a><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="random.html#Rand"><span class="Identifier">Rand</span></a><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- <p>The jump function for the generator.</p>
- <p>This proc is equivalent to <tt class="docutils literal"><span class="pre"><span class="DecNumber">2</span><span class="Operator">^</span><span class="DecNumber">64</span></span></tt> calls to <a class="reference external" href="#next,Rand">next</a>, and it can be used to generate <tt class="docutils literal"><span class="pre"><span class="DecNumber">2</span><span class="Operator">^</span><span class="DecNumber">64</span></span></tt> non-overlapping subsequences for parallel computations.</p>
- <p>When multiple threads are generating random numbers, each thread must own the <a class="reference external" href="#Rand">Rand</a> state it is using so that the thread can safely obtain random numbers. However, if each thread creates its own Rand state, the subsequences of random numbers that each thread generates may overlap, even if the provided seeds are unique. This is more likely to happen as the number of threads and amount of random numbers generated increases.</p>
- <p>If many threads will generate random numbers concurrently, it is better to create a single Rand state and pass it to each thread. After passing the Rand state to a thread, call this proc before passing it to the next one. By using the Rand state this way, the subsequences of random numbers generated in each thread will never overlap as long as no thread generates more than <tt class="docutils literal"><span class="pre"><span class="DecNumber">2</span><span class="Operator">^</span><span class="DecNumber">64</span></span></tt> random numbers.</p>
- <p><strong>See also:</strong></p>
- <ul class="simple"><li><a class="reference external" href="#next,Rand">next proc</a></li>
- </ul>
- <p><strong class="examples_text">Example: cmd: --threads:on</strong></p>
- <pre class="listing"><span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">random</span>
- <span class="Keyword">const</span> <span class="Identifier">numbers</span> <span class="Operator">=</span> <span class="DecNumber">100000</span>
- <span class="Keyword">var</span>
- <span class="Identifier">thr</span><span class="Punctuation">:</span> <span class="Identifier">array</span><span class="Punctuation">[</span><span class="FloatNumber">0.</span><span class="Operator">.</span><span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="Identifier">Thread</span><span class="Punctuation">[</span><span class="Punctuation">(</span><span class="Identifier">Rand</span><span class="Punctuation">,</span> <span class="Identifier">int</span><span class="Punctuation">)</span><span class="Punctuation">]</span><span class="Punctuation">]</span>
- <span class="Identifier">vals</span><span class="Punctuation">:</span> <span class="Identifier">array</span><span class="Punctuation">[</span><span class="FloatNumber">0.</span><span class="Operator">.</span><span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="Identifier">int</span><span class="Punctuation">]</span>
- <span class="Keyword">proc</span> <span class="Identifier">randomSum</span><span class="Punctuation">(</span><span class="Identifier">params</span><span class="Punctuation">:</span> <span class="Keyword">tuple</span><span class="Punctuation">[</span><span class="Identifier">r</span><span class="Punctuation">:</span> <span class="Identifier">Rand</span><span class="Punctuation">,</span> <span class="Identifier">index</span><span class="Punctuation">:</span> <span class="Identifier">int</span><span class="Punctuation">]</span><span class="Punctuation">)</span> <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">thread</span><span class="Operator">.</span><span class="Punctuation">}</span> <span class="Operator">=</span>
- <span class="Keyword">var</span> <span class="Identifier">r</span> <span class="Operator">=</span> <span class="Identifier">params</span><span class="Operator">.</span><span class="Identifier">r</span>
- <span class="Keyword">var</span> <span class="Identifier">s</span> <span class="Operator">=</span> <span class="DecNumber">0</span> <span class="Comment"># avoid cache thrashing</span>
- <span class="Keyword">for</span> <span class="Identifier">i</span> <span class="Keyword">in</span> <span class="FloatNumber">1.</span><span class="Operator">.</span><span class="Identifier">numbers</span><span class="Punctuation">:</span>
- <span class="Identifier">s</span> <span class="Operator">+=</span> <span class="Identifier">r</span><span class="Operator">.</span><span class="Identifier">rand</span><span class="Punctuation">(</span><span class="FloatNumber">0.</span><span class="Operator">.</span><span class="DecNumber">10</span><span class="Punctuation">)</span>
- <span class="Identifier">vals</span><span class="Punctuation">[</span><span class="Identifier">params</span><span class="Operator">.</span><span class="Identifier">index</span><span class="Punctuation">]</span> <span class="Operator">=</span> <span class="Identifier">s</span>
- <span class="Keyword">var</span> <span class="Identifier">r</span> <span class="Operator">=</span> <span class="Identifier">initRand</span><span class="Punctuation">(</span><span class="DecNumber">2019</span><span class="Punctuation">)</span>
- <span class="Keyword">for</span> <span class="Identifier">i</span> <span class="Keyword">in</span> <span class="FloatNumber">0.</span><span class="Operator">.<</span><span class="Identifier">thr</span><span class="Operator">.</span><span class="Identifier">len</span><span class="Punctuation">:</span>
- <span class="Identifier">createThread</span><span class="Punctuation">(</span><span class="Identifier">thr</span><span class="Punctuation">[</span><span class="Identifier">i</span><span class="Punctuation">]</span><span class="Punctuation">,</span> <span class="Identifier">randomSum</span><span class="Punctuation">,</span> <span class="Punctuation">(</span><span class="Identifier">r</span><span class="Punctuation">,</span> <span class="Identifier">i</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
- <span class="Identifier">r</span><span class="Operator">.</span><span class="Identifier">skipRandomNumbers</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
- <span class="Identifier">joinThreads</span><span class="Punctuation">(</span><span class="Identifier">thr</span><span class="Punctuation">)</span>
- <span class="Keyword">for</span> <span class="Identifier">val</span> <span class="Keyword">in</span> <span class="Identifier">vals</span><span class="Punctuation">:</span>
- <span class="Identifier">doAssert</span> <span class="Identifier">abs</span><span class="Punctuation">(</span><span class="Identifier">val</span> <span class="Operator">-</span> <span class="Identifier">numbers</span> <span class="Operator">*</span> <span class="DecNumber">5</span><span class="Punctuation">)</span> <span class="Operator">/</span> <span class="Identifier">numbers</span> <span class="Operator"><</span> <span class="FloatNumber">0.1</span>
- <span class="Identifier">doAssert</span> <span class="Identifier">vals</span> <span class="Operator">==</span> <span class="Punctuation">[</span><span class="DecNumber">501737</span><span class="Punctuation">,</span> <span class="DecNumber">497901</span><span class="Punctuation">,</span> <span class="DecNumber">500683</span><span class="Punctuation">,</span> <span class="DecNumber">500157</span><span class="Punctuation">]</span></pre>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L161" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L161" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- </dl>
- </div>
- <div class="section" id="18">
- <h1><a class="toc-backref" href="#18">Templates</a></h1>
- <dl class="item">
- <div id="randState-templates-all">
- <div id="randState.t">
- <dt><pre><span class="Keyword">template</span> <a href="#randState.t"><span class="Identifier">randState</span></a><span class="Other">(</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a></pre></dt>
- <dd>
-
- Makes the default Rand state accessible from other modules. Useful for module authors.
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/random.nim#L130" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/random.nim#L130" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- </dl>
- </div>
- </div>
- </div>
- <div class="twelve-columns footer">
- <span class="nim-sprite"></span>
- <br>
- <small style="color: var(--hint);">Made with Nim. Generated: 2025-03-25 06:49:47 UTC</small>
- </div>
- </div>
- </div>
- <script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
-
- </body>
- </html>
|