0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

paiza.ioでrust その3

Posted at

概要

paiza.ioでrustやってみた。
pcgで乱数を作り、zundokoやってみた。

参考にしたページ

サンプルコード


struct Rand {
    seed: u32
}
const RAND_MAX: u32 = 0xffff_ffff;
impl Rand {
    fn new(x: u32) -> Rand {
        Rand { seed: x }
    }
    fn rand(&mut self) -> u32 {
        let x = self.seed as u64;
        self.seed = ((69069 * x + 1) & RAND_MAX as u64) as u32;
        self.seed
    }
    fn random(&mut self) -> f64 {
        (1.0 / (RAND_MAX as f64 + 1.0)) * self.rand() as f64
    }
    fn shuffle<T>(&mut self, buff: &mut [T]) {
        for i in 0 .. buff.len() 
        {
            let j = (self.random() * buff.len() as f64) as usize;
            buff.swap(i, j);
        }
    }
}

fn main() {
    let mut rng = Rand::new(5);
    for _ in 0..3
    {
        println!("{}", rng.rand());
    }
    for _ in 0..3 
    {
        println!("{}", rng.random());
    }
    let mut buff = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
    rng.shuffle(&mut buff);
    println!("{:?}", buff);
    rng.shuffle(&mut buff);
    println!("{:?}", buff);
    rng.shuffle(&mut buff);
    println!("{:?}", buff);
    let mut zcnt = 0;
    loop 
    {
        let s = rng.random();
        let mut zd = false;
        if s < 0.5
        {
            zd = true;
        }
        print!("{} ", if zd {"ズン"} else {"ドコ"});
        if zd 
        {
            zcnt += 1
        }
        else if zcnt >= 4
        {
            break
        }
        else
        {
            zcnt = 0
        }
    }
    println!(" キ・ヨ・シ!");
}


実行結果

345346
2377866395
1599604512
0.8475697415415198
0.7944785314612091
0.8376894944813102
[3, 9, 5, 1, 0, 7, 2, 8, 4, 6]
[0, 1, 6, 9, 5, 2, 8, 3, 7, 4]
[1, 3, 9, 6, 2, 4, 5, 7, 8, 0]
ドコ ズン ドコ ドコ ズン ズン ズン ドコ ドコ ドコ ドコ ズン ドコ ドコ ズン ドコ ドコ ドコ ズン ドコ ドコ ズン ズン ズン ドコ ドコ ズン ズン ドコ ドコ ドコ ズン ズン ズン ズン ドコ  キ・ヨ・シ!


成果物

以上

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?