Anwender95 / go-tour

Automatically exported from code.google.com/p/go-tour
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

tree/tree.go always returns the same trees between subsequent runs #180

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1.Use http://127.0.0.1:3999/concurrency/8
2. use this code inside the main function:

   t := tree.New(1)
   fmt.Println(t)

What is the expected output? What do you see instead?
I would expect multiple runs to return trees with different structure.
Instead, they return the same tree, because the random number generator was not 
initialized.

What version of the product are you using? On what operating system?
214:18291d79a95f on Ubuntu 14.04 x86_64

Please provide any additional information below.

The following patch fixes it:

diff -r 18291d79a95f tree/tree.go
--- a/tree/tree.go  Fri Aug 29 09:12:58 2014 +1000
+++ b/tree/tree.go  Sun Aug 31 20:49:28 2014 +0200
@@ -7,6 +7,7 @@
 import (
    "fmt"
    "math/rand"
+   "time"
 )

 // A Tree is a binary tree with integer values.
@@ -19,6 +20,7 @@
 // New returns a new, random binary tree holding the values k, 2k, ..., 10k.
 func New(k int) *Tree {
    var t *Tree
+   rand.Seed(time.Now().UnixNano())
    for _, v := range rand.Perm(10) {
        t = insert(t, (1+v)*k)
    }

Original issue reported on code.google.com by cristi.m...@gmail.com on 31 Aug 2014 at 7:02

GoogleCodeExporter commented 8 years ago
Does it matter?

Original comment by a...@golang.org on 31 Aug 2014 at 11:29