Documentation
¶
Overview ¶
randパッケージは、シミュレーションなどのタスクに適した擬似乱数生成器を実装しますが、セキュリティに敏感な作業には使用しないでください。
乱数は Source によって生成され、通常は Rand でラップされます。 両方のタイプは一度に1つのゴルーチンから使用されるべきです:複数のゴルーチン間で共有するには何らかの同期が必要です。
トップレベルの関数、例えば Float64 や Int は、 複数のゴルーチンによる並行使用が安全です。
このパッケージの出力は、どのようにシードされていても容易に予測可能かもしれません。セキュリティに敏感な作業に適した乱数については、 crypto/rand パッケージを参照してください。
Example ¶
answers := []string{
"It is certain",
"It is decidedly so",
"Without a doubt",
"Yes definitely",
"You may rely on it",
"As I see it yes",
"Most likely",
"Outlook good",
"Yes",
"Signs point to yes",
"Reply hazy try again",
"Ask again later",
"Better not tell you now",
"Cannot predict now",
"Concentrate and ask again",
"Don't count on it",
"My reply is no",
"My sources say no",
"Outlook not so good",
"Very doubtful",
}
fmt.Println("Magic 8-Ball says:", answers[rand.IntN(len(answers))])
Example (Rand) ¶
この例は、*Randの各メソッドの使用を示しています。 グローバル関数の使用は、レシーバなしで同じです。
// ジェネレータを作成し、シードを設定します。
// 通常、固定されていないシードを使用するべきです。例えば、Uint64(), Uint64()のようなものです。
// 固定シードを使用すると、毎回同じ出力が生成されます。
r := rand.New(rand.NewPCG(1, 2))
// ここでのtabwriterは、私たちが整列した出力を生成するのを助けてくれます。
w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
defer w.Flush()
show := func(name string, v1, v2, v3 any) {
fmt.Fprintf(w, "%s\t%v\t%v\t%v\n", name, v1, v2, v3)
}
// Float32とFloat64の値は[0, 1)の範囲内にあります。
show("Float32", r.Float32(), r.Float32(), r.Float32())
show("Float64", r.Float64(), r.Float64(), r.Float64())
// ExpFloat64の値は平均が1ですが、指数関数的に減衰します。
show("ExpFloat64", r.ExpFloat64(), r.ExpFloat64(), r.ExpFloat64())
// NormFloat64の値は平均が0で、標準偏差が1です。
show("NormFloat64", r.NormFloat64(), r.NormFloat64(), r.NormFloat64())
// Int32、Int64、およびUint32は指定された幅の値を生成します。
// Intメソッド(ここでは示されていません)は'int'のサイズに応じてInt32またはInt64のように動作します。
show("Int32", r.Int32(), r.Int32(), r.Int32())
show("Int64", r.Int64(), r.Int64(), r.Int64())
show("Uint32", r.Uint32(), r.Uint32(), r.Uint32())
// IntN、Int32N、およびInt64Nは、出力がn未満になるように制限します。
// これらは、r.Int()%nを使用するよりも慎重に行います。
show("IntN(10)", r.IntN(10), r.IntN(10), r.IntN(10))
show("Int32N(10)", r.Int32N(10), r.Int32N(10), r.Int32N(10))
show("Int64N(10)", r.Int64N(10), r.Int64N(10), r.Int64N(10))
// Permは、数値[0, n)のランダムな順列を生成します。
show("Perm", r.Perm(5), r.Perm(5), r.Perm(5))
Output: Float32 0.95955694 0.8076733 0.8135684 Float64 0.4297927436037299 0.797802349388613 0.3883664855410056 ExpFloat64 0.43463410545541104 0.5513632046504593 0.7426404617374481 NormFloat64 -0.9303318111676635 -0.04750789419852852 0.22248301107582735 Int32 2020777787 260808523 851126509 Int64 5231057920893523323 4257872588489500903 158397175702351138 Uint32 314478343 1418758728 208955345 IntN(10) 6 2 0 Int32N(10) 3 7 7 Int64N(10) 8 9 4 Perm [0 3 1 4 2] [4 1 2 0 3] [4 3 2 0 1]
Index ¶
- func ExpFloat64() float64
- func Float32() float32
- func Float64() float64
- func Int() int
- func Int32() int32
- func Int32N(n int32) int32
- func Int64() int64
- func Int64N(n int64) int64
- func IntN(n int) int
- func N[Int intType](n Int) Int
- func NormFloat64() float64
- func Perm(n int) []int
- func Shuffle(n int, swap func(i, j int))
- func Uint() uint
- func Uint32() uint32
- func Uint32N(n uint32) uint32
- func Uint64() uint64
- func Uint64N(n uint64) uint64
- func UintN(n uint) uint
- type ChaCha8
- type PCG
- type Rand
- func (r *Rand) ExpFloat64() float64
- func (r *Rand) Float32() float32
- func (r *Rand) Float64() float64
- func (r *Rand) Int() int
- func (r *Rand) Int32() int32
- func (r *Rand) Int32N(n int32) int32
- func (r *Rand) Int64() int64
- func (r *Rand) Int64N(n int64) int64
- func (r *Rand) IntN(n int) int
- func (r *Rand) NormFloat64() float64
- func (r *Rand) Perm(n int) []int
- func (r *Rand) Shuffle(n int, swap func(i, j int))
- func (r *Rand) Uint() uint
- func (r *Rand) Uint32() uint32
- func (r *Rand) Uint32N(n uint32) uint32
- func (r *Rand) Uint64() uint64
- func (r *Rand) Uint64N(n uint64) uint64
- func (r *Rand) UintN(n uint) uint
- type Source
- type Zipf
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExpFloat64 ¶
func ExpFloat64() float64
ExpFloat64は、デフォルトのSourceからレートパラメータ(lambda)が1で平均が1/lambda(1)の指数分布に従う 範囲(0, +math.MaxFloat64]の指数分布のfloat64を返します。 異なるレートパラメータの分布を生成するために、呼び出し元は出力を調整できます:
sample = ExpFloat64() / desiredRateParameter
func IntN ¶
IntNは、デフォルトのSourceから半開放区間[0,n)内の非負の擬似乱数をintとして返します。 nが0以下の場合、パニックを引き起こします。
Example ¶
fmt.Println(rand.IntN(100)) fmt.Println(rand.IntN(100)) fmt.Println(rand.IntN(100))
func N ¶
func N[Int intType](n Int) Int
Nは、デフォルトのSourceから半開放区間[0,n)内の擬似乱数を返します。 型パラメータIntは任意の整数型にすることができます。 nが0以下の場合、パニックを引き起こします。
Example ¶
// 半開放区間[0, 100)内のint64を出力します。 fmt.Println(rand.N(int64(100))) // 0から100ミリ秒の間のランダムな期間スリープします。 time.Sleep(rand.N(100 * time.Millisecond))
func NormFloat64 ¶
func NormFloat64() float64
NormFloat64は、デフォルトのSourceから標準正規分布(平均 = 0、標準偏差 = 1)に従う 範囲[-math.MaxFloat64, +math.MaxFloat64]の正規分布のfloat64を返します。 異なる正規分布を生成するために、呼び出し元は出力を調整できます:
sample = NormFloat64() * desiredStdDev + desiredMean
func Perm ¶
Permは、デフォルトのSourceから半開放区間[0,n)内の整数の擬似乱数順列をn個のintのスライスとして返します。
Example ¶
for _, value := range rand.Perm(3) {
fmt.Println(value)
}
Output: 1 2 0
func Shuffle ¶
ShuffleはデフォルトのSourceを使用して要素の順序を擬似ランダムにします。 nは要素の数です。n < 0の場合、Shuffleはパニックを引き起こします。 swapは、インデックスiとjの要素を交換します。
Example ¶
words := strings.Fields("ink runs from the corners of my mouth")
rand.Shuffle(len(words), func(i, j int) {
words[i], words[j] = words[j], words[i]
})
fmt.Println(words)
Example (SlicesInUnison) ¶
numbers := []byte("12345")
letters := []byte("ABCDE")
// 数字をシャッフルし、同時にlettersの対応するエントリを交換します。
rand.Shuffle(len(numbers), func(i, j int) {
numbers[i], numbers[j] = numbers[j], numbers[i]
letters[i], letters[j] = letters[j], letters[i]
})
for i := range numbers {
fmt.Printf("%c: %c\n", letters[i], numbers[i])
}
Types ¶
type ChaCha8 ¶
type ChaCha8 struct {
// contains filtered or unexported fields
}
ChaCha8は、ChaCha8ベースの暗号的に強力な 乱数生成器です。
func NewChaCha8 ¶
NewChaCha8は、指定されたシードで初期化された新しいChaCha8を返します。
func (*ChaCha8) AppendBinary ¶ added in v1.25.0
AppendBinaryは encoding.BinaryAppender インターフェースを実装します。
func (*ChaCha8) MarshalBinary ¶
MarshalBinaryは encoding.BinaryMarshaler インターフェースを実装します。
func (*ChaCha8) Read ¶ added in v1.23.0
Readは、pに正確にlen(p)バイトを読み込みます。 常にlen(p)とnilエラーを返します。
ReadとUint64の呼び出しが交互に行われる場合、 両者によって返されるビットの順序は未定義であり、 Readは最後のUint64の呼び出し前に生成されたビットを返すことがあります。
func (*ChaCha8) UnmarshalBinary ¶
UnmarshalBinaryは encoding.BinaryUnmarshaler インターフェースを実装します。
type PCG ¶
type PCG struct {
// contains filtered or unexported fields
}
PCGは、128ビットの内部状態を持つPCGジェネレータです。 ゼロのPCGは、NewPCG(0, 0)と同等です。
func (*PCG) AppendBinary ¶ added in v1.25.0
AppendBinaryは encoding.BinaryAppender インターフェースを実装します。
func (*PCG) MarshalBinary ¶
MarshalBinaryは encoding.BinaryMarshaler インターフェースを実装します。
func (*PCG) UnmarshalBinary ¶
UnmarshalBinaryは encoding.BinaryUnmarshaler インターフェースを実装します。
type Rand ¶
type Rand struct {
// contains filtered or unexported fields
}
Randは、乱数のソースです。
func (*Rand) ExpFloat64 ¶
ExpFloat64は、レートパラメータ(lambda)が1で平均が1/lambda(1)の指数分布に従う 範囲(0, +math.MaxFloat64]の指数分布のfloat64を返します。 異なるレートパラメータの分布を生成するために、呼び出し元は出力を調整できます:
sample = ExpFloat64() / desiredRateParameter
func (*Rand) NormFloat64 ¶
NormFloat64は、標準正規分布(平均 = 0、標準偏差 = 1)に従う 範囲[-math.MaxFloat64, +math.MaxFloat64]の正規分布のfloat64を返します。 異なる正規分布を生成するために、呼び出し元は出力を調整できます:
sample = NormFloat64() * desiredStdDev + desiredMean
func (*Rand) Shuffle ¶
Shuffleは要素の順序を擬似ランダムにします。 nは要素の数です。n < 0の場合、Shuffleはパニックを引き起こします。 swapは、インデックスiとjの要素を交換します。
type Source ¶
type Source interface {
Uint64() uint64
}
Sourceは、範囲[0, 1<<64)内の一様に分布した 擬似乱数uint64値のソースです。
Sourceは、複数のゴルーチンによる並行使用には安全ではありません。