]> Cypherpunks repositories - gostls13.git/commitdiff
crypto/rand: add example for Int
authorAlan Yee <alanyee@users.noreply.github.com>
Fri, 21 Feb 2025 20:37:59 +0000 (20:37 +0000)
committerGopher Robot <gobot@golang.org>
Fri, 21 Feb 2025 23:24:41 +0000 (15:24 -0800)
Change-Id: I401e6092b1cbbd332406ffa7f923c46de0a3ff49
GitHub-Last-Rev: ef2057888465be3cff7fb60bc6ffe3d7573ec348
GitHub-Pull-Request: golang/go#71773
Reviewed-on: https://go-review.googlesource.com/c/go/+/650035
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
src/crypto/rand/example_test.go

index f0127749f0cf909e85f9b5910b39aab296da8bfe..64e38b11ce5737f57969c5dc9265712c4b339888 100644 (file)
@@ -4,7 +4,21 @@
 
 package rand_test
 
-import "crypto/rand"
+import (
+       "crypto/rand"
+       "fmt"
+       "math/big"
+)
+
+// ExampleInt prints a single cryptographically secure pseudorandom number between 0 and 99 inclusive.
+func ExampleInt() {
+       a, err := rand.Int(rand.Reader, big.NewInt(100))
+       if err != nil {
+               fmt.Println("error:", err)
+               return
+       }
+       fmt.Println(a.Int64())
+}
 
 func ExampleRead() {
        // Note that no error handling is necessary, as Read always succeeds.