rand

package
v1.21.10 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package randは、暗号学的に安全な乱数生成器を実装しています。

Index

Examples

Constants

This section is empty.

Variables

View Source
var Reader io.Reader

Readerは暗号学的に安全な乱数生成器のグローバルで共有されたインスタンスです。

Linux、FreeBSD、Dragonfly、NetBSD、Solarisでは、Readerは利用可能な場合はgetrandom(2)を使用し、 それ以外の場合は/dev/urandomを使用します。 OpenBSDとmacOSでは、Readerはgetentropy(2)を使用します。 他のUnix系システムでは、Readerは/dev/urandomから読み取ります。 Windowsシステムでは、ReaderはProcessPrng APIを使用します。 JS/Wasmでは、ReaderはWeb Crypto APIを使用します。 WASIP1/Wasmでは、Readerはwasi_snapshot_preview1からのrandom_getを使用します。

Functions

func Int

func Int(rand io.Reader, max *big.Int) (n *big.Int, err error)

Intは[0、max)の一様乱数を返します。max <= 0の場合、パニックを発生させます。

func Prime

func Prime(rand io.Reader, bits int) (*big.Int, error)

Prime は与えられたビット長の数が高確率で素数である場合に返します。 Prime は rand.Read によって返されるエラーまたは bits < 2 の場合にはエラーを返します。

func Read

func Read(b []byte) (n int, err error)

Readはio.ReadFullを使ってReader.Readを呼び出すヘルパー関数です。 帰り値として、n == len(b) は err == nil の場合に限り成り立ちます。

Example

この例は、rand.Readerから暗号的に安全な疑似乱数を10個読み込み、バイトスライスに書き込みます。

package main

import (
	"github.com/shogo82148/std/bytes"
	"github.com/shogo82148/std/crypto/rand"
	"github.com/shogo82148/std/fmt"
)

func main() {
	c := 10
	b := make([]byte, c)
	_, err := rand.Read(b)
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	// スライスは、ゼロの代わりにランダムなバイトを含んでいるべきです。
	fmt.Println(bytes.Equal(b, make([]byte, c)))

}
Output:

false

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL