Hi, in xrand() you are initialising r with the same constant seed for every call, so you always get the same pseudo random number.
See: <https://play.golang.org/p/XwNmYv5nzJ> cheers messju On Mon, May 08, 2017 at 03:26:08AM -0700, sivarajshabin...@gmail.com wrote: > Hi All, > > Firstly, sorry for the title that sounds like a click bait. I am not > understanding what exactly is wrong with this program > [1]https://play.golang.org/p/Tl0wpaCqK7 > > package main > > import ( > "fmt" > "math/rand" > "runtime" > ) > > func xrand() int { > r := rand.New(rand.NewSource(99)) > return r.Int() > } > > func main() { > fmt.Printf("Version %s\n", runtime.Version()) > fmt.Printf("looks buggy \t1: %d, 2: %d, 3: %d\n", xrand(), xrand(), > xrand()) > > r1 := rand.New(rand.NewSource(99)) > fmt.Printf("Looks fine \t1: %d, 2: %d, 3: %d\n", r1.Int(), > r1.Int(), r1.Int()) > } > Output>>>> > > Version go1.8 > looks buggy 1: 1108929909, 2: 1108929909, 3: 1108929909 > Looks find 1: 1108929909, 2: 1298032409, 3: 913431059 > > When I generate a random number with "math/rand", like the example given > in documentation [2]https://play.golang.org/p/O2k_za0AW7 except that I > return the randomly generated integer from a function, But the function > keeps returning the same value any number of time I call it. On the > contrary running the same code from the main generates random values, > there isn't a reason to doubt rand package. However in the second print > statement where random numbers are properly generated the first value > always is the same value returned by xrand(), here there is something to > doubt rand package. > > What am I missing here? Could it be a bug? > > Thanks in advance. > > Cheers > Shabinesh -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.