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).
Thanks bra

ReplyDeletepackage main

ReplyDeleteimport(

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

This helped me. Thank you.

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

ReplyDeleteSomeone 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!

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.

DeleteI would rather go with following:

ReplyDeleterand.Seed(time.Now().UTC().UnixNano())

return rand.Intn(max - min) + min

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

DeleteThank you so much.

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

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

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

ReplyDeleteThis comment has been removed by the author.

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

ReplyDeleteThe correct formula is:

rand.Intn((max - min) + 1) + min

This comment has been removed by the author.

ReplyDelete