Documentation
¶
Overview ¶
randパッケージは、シミュレーションなどのタスクに適した擬似乱数生成器を実装しますが、 セキュリティに敏感な作業には使用しないでください。
乱数は[Source]によって生成され、通常は Rand でラップされます。 これらの型は一度に1つのゴルーチンで使用する必要があります:複数のゴルーチン間で共有するには何らかの同期が必要です。
トップレベルの関数、たとえば Float64 や Int などは、 複数のゴルーチンによる並行使用に対して安全です。
このパッケージの出力は、どのようにシードされていても容易に予測可能かもしれません。 セキュリティに敏感な作業に適したランダムな数値については、crypto/randパッケージを参照してください。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/math/rand" ) func main() { 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の各メソッドの使用を示しています。 グローバル関数の使用は、レシーバーなしで同じです。
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/math/rand" "github.com/shogo82148/std/os" "github.com/shogo82148/std/text/tabwriter" ) func main() { // ジェネレータを作成し、シードを設定します。 // 通常、固定されていないシードを使用するべきで、例えばtime.Now().UnixNano()などです。 // 固定されたシードを使用すると、毎回の実行で同じ出力が生成されます。 r := rand.New(rand.NewSource(99)) // ここでの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()) // Int31、Int63、およびUint32は、指定された幅の値を生成します。 // Intメソッド(ここでは示されていません)は、'int'のサイズに応じてInt31またはInt63のどちらかと同様です。 show("Int31", r.Int31(), r.Int31(), r.Int31()) show("Int63", r.Int63(), r.Int63(), r.Int63()) show("Uint32", r.Uint32(), r.Uint32(), r.Uint32()) // Intn、Int31n、およびInt63nは、出力をn未満に制限します。 // これらは、r.Int()%nを使用するよりも慎重に行います。 show("Intn(10)", r.Intn(10), r.Intn(10), r.Intn(10)) show("Int31n(10)", r.Int31n(10), r.Int31n(10), r.Int31n(10)) show("Int63n(10)", r.Int63n(10), r.Int63n(10), r.Int63n(10)) // Permは、数値[0, n)のランダムな順列を生成します。 show("Perm", r.Perm(5), r.Perm(5), r.Perm(5)) }
Output: Float32 0.2635776 0.6358173 0.6718283 Float64 0.628605430454327 0.4504798828572669 0.9562755949377957 ExpFloat64 0.3362240648200941 1.4256072328483647 0.24354758816173044 NormFloat64 0.17233959114940064 1.577014951434847 0.04259129641113857 Int31 1501292890 1486668269 182840835 Int63 3546343826724305832 5724354148158589552 5239846799706671610 Uint32 2760229429 296659907 1922395059 Intn(10) 1 2 5 Int31n(10) 4 7 8 Int63n(10) 7 6 3 Perm [1 4 2 3 0] [4 2 1 3 0] [1 2 4 0 3]
Index ¶
- func ExpFloat64() float64
- func Float32() float32
- func Float64() float64
- func Int() int
- func Int31() int32
- func Int31n(n int32) int32
- func Int63() int64
- func Int63n(n int64) int64
- func Intn(n int) int
- func NormFloat64() float64
- func Perm(n int) []int
- func Read(p []byte) (n int, err error)deprecated
- func Seed(seed int64)deprecated
- func Shuffle(n int, swap func(i, j int))
- func Uint32() uint32
- func Uint64() uint64
- 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) Int31() int32
- func (r *Rand) Int31n(n int32) int32
- func (r *Rand) Int63() int64
- func (r *Rand) Int63n(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) Read(p []byte) (n int, err error)
- func (r *Rand) Seed(seed int64)
- func (r *Rand) Shuffle(n int, swap func(i, j int))
- func (r *Rand) Uint32() uint32
- func (r *Rand) Uint64() uint64
- type Source
- type Source64
- type Zipf
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExpFloat64 ¶
func ExpFloat64() float64
ExpFloat64は、デフォルトの Source から、範囲 (0, +[math.MaxFloat64]]内の指数分布に従うfloat64を返します。 レートパラメータ(ラムダ)が1で、平均が1/ラムダ(1)の指数分布です。 異なるレートパラメータの分布を生成するために、 呼び出し元は出力を調整することができます:
sample = ExpFloat64() / desiredRateParameter
func Intn ¶
Intnは、デフォルトの Source から半開区間[0,n)内の非負の擬似乱数をintとして返します。 nが0以下の場合、パニックを引き起こします。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/math/rand" ) func main() { fmt.Println(rand.Intn(100)) fmt.Println(rand.Intn(100)) fmt.Println(rand.Intn(100)) }
func NormFloat64 ¶
func NormFloat64() float64
NormFloat64は、デフォルトの Source から、範囲 [[-math.MaxFloat64], +[math.MaxFloat64]]内の正規分布に従うfloat64を返します。 標準正規分布(平均 = 0、標準偏差 = 1)です。 異なる正規分布を生成するために、呼び出し元は 出力を調整することができます:
sample = NormFloat64() * desiredStdDev + desiredMean
func Perm ¶
Permは、デフォルトの Source から半開区間[0,n)内の整数の擬似乱数順列を、n個のintのスライスとして返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/math/rand" ) func main() { for _, value := range rand.Perm(3) { fmt.Println(value) } }
Output: 1 2 0
func Read
deprecated
added in
v1.6.0
Readは、デフォルトの Source からlen(p)個のランダムなバイトを生成し、それらをpに書き込みます。 常にlen(p)とnilエラーを返します。 Readは、Rand.Read メソッドとは異なり、並行使用に安全です。
Deprecated: ほとんどの使用ケースでは、crypto/rand.Read の方が適切です。 決定論的なソースが必要な場合は、math/rand/v2.ChaCha8.Read を使用してください。
func Seed
deprecated
func Seed(seed int64)
Seedは、提供されたシード値を使用してデフォルトのSourceを 決定的な状態に初期化します。2³¹-1で割った余りが同じであるシード値は、 同じ擬似乱数系列を生成します。 Seedは、Rand.Seed メソッドとは異なり、並行使用に安全です。
Seedが呼び出されない場合、ジェネレータはプログラムの起動時にランダムにシードされます。
Go 1.20より前では、ジェネレータはプログラムの起動時にSeed(1)のようにシードされました。 古い振る舞いを強制するには、プログラムの起動時にSeed(1)を呼び出します。 あるいは、このパッケージの関数を呼び出す前に環境変数でGODEBUG=randautoseed=0を設定します。
Deprecated: Go 1.20以降、ランダムな値でSeedを呼び出す理由はありません。 特定の結果のシーケンスを得るために既知の値でSeedを呼び出すプログラムは、 New(NewSource(seed))を使用してローカルのランダムジェネレータを取得するべきです。
func Shuffle ¶ added in v1.10.0
Shuffleはデフォルトの Source を使用して要素の順序を擬似ランダムにします。 nは要素の数です。n < 0の場合、Shuffleはパニックを引き起こします。 swapは、インデックスiとjの要素を交換します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/math/rand" "github.com/shogo82148/std/strings" ) func main() { 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) ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/math/rand" ) func main() { 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 Rand ¶
type Rand struct {
// contains filtered or unexported fields
}
Randは、乱数のソースです。
func (*Rand) ExpFloat64 ¶
ExpFloat64は、レートパラメータ(ラムダ)が1で、平均が1/ラムダ(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) Read ¶ added in v1.6.0
Readは、len(p)個のランダムなバイトを生成し、それらをpに書き込みます。 常にlen(p)とnilエラーを返します。 Readは、他のRandメソッドと同時に呼び出すべきではありません。
type Source ¶
Sourceは、範囲[0, 1<<63)内の一様に分布した 擬似乱数int64値のソースを表します。
Sourceは、複数のゴルーチンによる並行使用には安全ではありません。
type Source64 ¶ added in v1.8.0
Source64は、範囲[0, 1<<64)内の一様に分布した 擬似乱数uint64値を直接生成することもできる Source です。 Rand rの基礎となる Source sがSource64を実装している場合、 r.Uint64はs.Int63を2回呼び出す代わりに、s.Uint64を1回呼び出した結果を返します。