Merge branch 'rand'

This commit is contained in:
Mark McGranaghan 2015-07-27 11:13:56 -07:00
commit 4131f6b3bb
4 changed files with 20 additions and 12 deletions

View File

@ -4,6 +4,7 @@
package main
import "time"
import "fmt"
import "math/rand"
@ -25,9 +26,12 @@ func main() {
fmt.Print((rand.Float64() * 5) + 5)
fmt.Println()
// To make the pseudorandom generator deterministic,
// give it a well-known seed.
s1 := rand.NewSource(42)
// The default number generator is deterministic, so it'll
// produce the same sequence of numbers each time by default.
// To produce varying sequences, give it a seed that changes.
// Note that this is not safe to use for random numbers you
// intend to be secret, use `crypto/rand` for those.
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)
// Call the resulting `rand.Rand` just like the

View File

@ -1,2 +1,2 @@
f7ce9724ab51b3e90b8d04cf88eaaa8cf2dfd50e
X973CNW5aa
6ce1f98d3c22ee8bf02c41b393361d6cd5fff23a
TZElZIwNU1

View File

@ -1,4 +1,4 @@
$ go run random-numbers.go
$ go run random-numbers.go
81,87
0.6645600532184904
7.123187485356329,8.434115364335547

View File

@ -41,7 +41,7 @@ generation.</p>
</td>
<td class="code leading">
<a href="http://play.golang.org/p/X973CNW5aa"><img title="Run code" src="play.png" class="run" /></a>
<a href="http://play.golang.org/p/TZElZIwNU1"><img title="Run code" src="play.png" class="run" /></a>
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
</pre></div>
@ -54,7 +54,8 @@ generation.</p>
</td>
<td class="code leading">
<div class="highlight"><pre><span class="kn">import</span> <span class="s">&quot;fmt&quot;</span>
<div class="highlight"><pre><span class="kn">import</span> <span class="s">&quot;time&quot;</span>
<span class="kn">import</span> <span class="s">&quot;fmt&quot;</span>
<span class="kn">import</span> <span class="s">&quot;math/rand&quot;</span>
</pre></div>
@ -121,13 +122,16 @@ other ranges, for example <code>5.0 &lt;= f' &lt; 10.0</code>.</p>
<tr>
<td class="docs">
<p>To make the pseudorandom generator deterministic,
give it a well-known seed.</p>
<p>The default number generator is deterministic, so it&rsquo;ll
produce the same sequence of numbers each time by default.
To make it non-deterministic, give it a seed that changes.
Note that this is not safe to use for random numbers you
intend to be secret, use <code>crypto/rand</code> for those.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nx">s1</span> <span class="o">:=</span> <span class="nx">rand</span><span class="p">.</span><span class="nx">NewSource</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<div class="highlight"><pre> <span class="nx">s1</span> <span class="o">:=</span> <span class="nx">rand</span><span class="p">.</span><span class="nx">NewSource</span><span class="p">(</span><span class="nx">time</span><span class="p">.</span><span class="nx">Now</span><span class="p">().</span><span class="nx">UnixNano</span><span class="p">())</span>
<span class="nx">r1</span> <span class="o">:=</span> <span class="nx">rand</span><span class="p">.</span><span class="nx">New</span><span class="p">(</span><span class="nx">s1</span><span class="p">)</span>
</pre></div>
@ -179,7 +183,7 @@ produces the same sequence of random numbers.</p>
</td>
<td class="code leading">
<div class="highlight"><pre><span class="gp">$</span> go run random-numbers.go
<div class="highlight"><pre><span class="gp">$</span> go run random-numbers.go
<span class="go">81,87</span>
<span class="go">0.6645600532184904</span>
<span class="go">7.123187485356329,8.434115364335547</span>