Every call of xrand calls .Int() on a fresh generator initialised
the same way as the previous one, so it will give the same
result. The calls to .Int() in main are all on the /same/
generator, which is updated by each call, so you get
three different values.

Chris



On 8 May 2017 at 11:26,  <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
> 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 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.



-- 
Chris "allusive" Dollin

-- 
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.

Reply via email to