rand3.go 577 B

1234567891011121314151617181920212223242526272829
  1. package main
  2. import (
  3. "crypto/rand"
  4. "fmt"
  5. "math/big"
  6. "strings"
  7. "strconv"
  8. )
  9. // Outputs cryptographically secure pseudo random number between min and max
  10. // using crypt/rand.
  11. func randomNumber(min, max int64) int64 {
  12. num, e := rand.Int(rand.Reader, big.NewInt(int64(max)))
  13. if e != nil {
  14. fmt.Print(e)
  15. }
  16. return num.Int64() + min
  17. }
  18. func main() {
  19. var sb strings.Builder
  20. for i := 0; i < 5; i++ {
  21. sb.WriteString( strconv.FormatInt(randomNumber(1, 6), 10) )
  22. }
  23. // Show a Diceware random number
  24. fmt.Println(sb.String())
  25. }