commit bf11085207109e8841f9cc44e68d112c66726dca Author: Georg Pfuetzenreuter Date: Sat Sep 28 20:41:47 2024 +0200 Init Signed-off-by: Georg Pfuetzenreuter diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..63be32a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +go-overflow-sample diff --git a/README.md b/README.md new file mode 100644 index 0000000..85b1c44 --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +``` +$ go build + +$ ./go-overflow-sample 999999999999999999 +1492596583345867513 + +$ ./go-overflow-sample 9999999999999999999 +Could not convert argument to int64 +panic: crypto/rand: argument to Int is <= 0 + +goroutine 1 [running]: +crypto/rand.Int({0x4d0b88?, 0xc000014060?}, 0x0?) + /usr/lib64/go/1.22/src/crypto/rand/util.go:64 +0x2ca +main.RandInt(0x4d0b08?) + /home/georg/Work/git/go-overflow-sample/main.go:23 +0x86 +main.main() + /home/georg/Work/git/go-overflow-sample/main.go:18 +0xdc +``` diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c6adf8b --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module go-overflow-sample + +go 1.22.6 diff --git a/main.go b/main.go new file mode 100644 index 0000000..ba5c655 --- /dev/null +++ b/main.go @@ -0,0 +1,28 @@ +package main + +import "crypto/rand" +import "math/big" +import "fmt" +import "os" +import "strconv" + +func main() { + if len(os.Args) < 2 { + fmt.Println("Pass a number") + os.Exit(1) + } + number, err := strconv.Atoi(os.Args[1]) + if err != nil { + fmt.Println("Could not convert argument to int64") + } + RandInt(int64(number * 100)) + +} + +func RandInt(max int64) { + i, err := rand.Int(rand.Reader, big.NewInt(int64(max))) + if err != nil { + fmt.Println("Could not randomize") + } + fmt.Println(i.Uint64()) +}