Movatterモバイル変換


[0]ホーム

URL:


rand

packagestandard library
go1.25.2Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 7, 2025 License:BSD-3-ClauseImports:10Imported by:196,561

Details

Repository

cs.opensource.google/go/go

Links

Documentation

Overview

Package rand implements a cryptographically securerandom number generator.

Index

Examples

Constants

This section is empty.

Variables

Reader is a global, shared instance of a cryptographicallysecure random number generator. It is safe for concurrent use.

  • On Linux, FreeBSD, Dragonfly, and Solaris, Reader uses getrandom(2).
  • On legacy Linux (< 3.17), Reader opens /dev/urandom on first use.
  • On macOS, iOS, and OpenBSD Reader, uses arc4random_buf(3).
  • On NetBSD, Reader uses the kern.arandom sysctl.
  • On Windows, Reader uses the ProcessPrng API.
  • On js/wasm, Reader uses the Web Crypto API.
  • On wasip1/wasm, Reader uses random_get.

In FIPS 140-3 mode, the output passes through an SP 800-90A Rev. 1Deterministric Random Bit Generator (DRBG).

Functions

funcInt

func Int(randio.Reader, max *big.Int) (n *big.Int, errerror)

Int returns a uniform random value in [0, max). It panics if max <= 0, andreturns an error if rand.Read returns one.

Example

ExampleInt prints a single cryptographically secure pseudorandom number between 0 and 99 inclusive.

package mainimport ("crypto/rand""fmt""math/big")func main() {// Int cannot return an error when using rand.Reader.a, _ := rand.Int(rand.Reader, big.NewInt(100))fmt.Println(a.Int64())}

funcPrime

func Prime(randio.Reader, bitsint) (*big.Int,error)

Prime returns a number of the given bit length that is prime with high probability.Prime will return error for any error returned by rand.Read or if bits < 2.

Example

ExamplePrime prints a cryptographically secure pseudorandom 64 bit prime number.

package mainimport ("crypto/rand""fmt")func main() {// Prime cannot return an error when using rand.Reader and bits >= 2.a, _ := rand.Prime(rand.Reader, 64)fmt.Println(a.Int64())}

funcRead

func Read(b []byte) (nint, errerror)

Read fills b with cryptographically secure random bytes. It never returns anerror, and always fills b entirely.

Read callsio.ReadFull onReader and crashes the program irrecoverably ifan error is returned. The default Reader uses operating system APIs that aredocumented to never return an error on all but legacy Linux systems.

Example

ExampleRead prints a cryptographically secure pseudorandom 32 byte key.

package mainimport ("crypto/rand""fmt")func main() {// Note that no error handling is necessary, as Read always succeeds.key := make([]byte, 32)rand.Read(key)// The key can contain any byte value, print the key in hex.fmt.Printf("% x\n", key)}

funcTextadded ingo1.24.0

func Text()string

Text returns a cryptographically random string using the standardRFC 4648 base32 alphabetfor use when a secret string, token, password, or other text is needed.The result contains at least 128 bits of randomness, enough to prevent brute forceguessing attacks and to make the likelihood of collisions vanishingly small.A future version may return longer texts as needed to maintain those properties.

Example

ExampleText prints a random key encoded in base32.

package mainimport ("crypto/rand""fmt")func main() {key := rand.Text()// The key is base32 and safe to display.fmt.Println(key)}

Types

This section is empty.

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp