5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Rust] rand でシードを指定

Last updated at Posted at 2019-07-25

rand 0.7.0 でシードを指定して乱数を生成する例. (コピペ用) 2020-01-28 rand_xoshiro のバージョンを上げた

Cargo.toml
[dependencies]
rand = "0.7"
rand_xoshiro = "0.4"
src/main.rs
use rand::{Rng, SeedableRng};

fn main() {
    let mut rng = rand_xoshiro::Xoshiro256StarStar::seed_from_u64(123);

    for _ in 0..8 {
        println!("{}", rng.gen::<u8>());
    }
}

数値計算を想定して, 暗号学的に安全ではないが, 高速に動作する長周期 PRNG である Xoshiro256** を採用しています. 詳しい情報は rand の公式ガイドの このあたり とか このあたり 参照. rand の基礎は この記事 が詳しいです.

おまけ: 並列計算するとき

let rng1 = rand_xoshiro::Xoshiro256StarStar::seed_from_u64(0);
let mut rng2 = rng1.clone();
rng2.jump();

みたいな感じで RNG を増やしてそれを各スレッドで使う ( 参考文献).

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?