### generate random number in a given range

Total noob, teaching myself how to code.

I just spent an hour trying to figure out how to generate a random number from a given range using the go programming language(golang).

Here's the best that I've found so far...

```package main
import(
"fmt"
"math/rand"
"time"
)

func random(min, max int) int {
rand.Seed(time.Now().Unix())
return rand.Intn(max - min) + min
}

func main() {
myrand := random(1, 6)
fmt.Println(myrand)
}
```

`func random` defines how to create the random number.
`rand.Seed` ensures that the number that is generated is random(almost).
`func main` gives you that random number within any two positive numbers that you specify (in this case, 1 and 6).

1. package main
import(
"fmt"
"math/rand"
"time"
)

func random(min, max int) int {
return rand.Intn(max - min) + min
}

func main() {
rand.Seed(time.Now().Unix())
myrand := random(1, 6)
fmt.Println(myrand)
}

rand.Seed gets reset when called so you call it usually once when the program starts. The above code will result in a different random number each time your random function is called.

1. This helped me. Thank you.

2. This code is vulnerable to a time attack. Given knowledge of the seed, the output is deterministic. Bad idea.

Someone could create their own program that will produce identical outputs to your program, allowing them to know your "random" number. Its fine if you are only using it for learning, but dont use it in production.

If you are on linux, you could poll "/dev/random" or "/dev/urandom" and have that be your seed. Or better yet, use crypto/rand.

Good luck and have fun!

1. This code is perfect for statistical calculations, including production use. Note import "math/rand" vs "crypto/rand", that allows one easily distinguish the purpose of random generator and intendent use of the program.

3. I would rather go with following:

rand.Seed(time.Now().UTC().UnixNano())
return rand.Intn(max - min) + min

1. Thanks a lot! This is better because it does not produce the same number with multiple successive runs

2. Thank you so much.

4. the code above will never return a 6, rand.Intn(n) returns a value between 0 and n-1. To prove this to myself I modified the above examples to run a loop 10k times looking for a target number. 6 never is never returned.

package main
import(
"fmt"
"math/rand"
"time"
"os"
"strconv"
)

func random(min, max int) int {
return rand.Intn(max - min) + min
}

func main() {
if len(os.Args) < 2 {
fmt.Println("Need a target Value between 1 and 6 as an argument.")
os.Exit(1)
}
target, err := strconv.Atoi(os.Args[1])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
rand.Seed(time.Now().Unix())
for i := 1; i < 10001; i++ {
myrand := random(1, 6)
if myrand == target {
fmt.Printf("It took %d trie(s) to get a %d\n",i,target)
os.Exit(0)
}
}
fmt.Printf("I have up on the %d after 10000 tries.\n", target)
}

to make it easier to test i took the target number from the arguments passed to my code at runtime.

5. Can someone tell me how to do the same for big.Int let say 40 digits number?

6. This comment has been removed by the author.

7. As Ed has shown this code is incorrect. You'll never get 6. max - min (6 - 1) gives you 5. The Intn function will return a random int between 0 and (n - 1). In this case n is 5 so the range is between 0 and 4. The highest number you'll ever get back is 4 from the Intn function. And then you're only adding min (1). So if the highest number you get back from Intn is 4 and you're only adding 1 then the highest number this random function will return is 5.

The correct formula is:
rand.Intn((max - min) + 1) + min

8. This comment has been removed by the author.