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 その4

Posted at

概要

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

サンプルコード


use std::io;

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 start_yubaba() {
    let mut rng = Rand::new(5);
    let mut origin_name = String::new();
    println!("契約書だよ、そこに名前を書きな。");
    io::stdin().read_line(&mut origin_name).expect("Failed to read line");
    origin_name = origin_name.trim().to_string();
    println!("ふん、{}というのかい、贅沢な名だね", origin_name);
    let name_length = origin_name.chars().count();
    let new_name_index = rng.rand() as usize % name_length;
    let new_name = origin_name.chars().nth(new_name_index).unwrap();
    println!("今からお前の名前は{}だ、いいかい、わかったら返事をするんだ!{}!!", new_name, new_name)
}
fn main() {
    start_yubaba();
}


実行結果

契約書だよ、そこに名前を書きな。
ふん、腹美恵子というのかい、贅沢な名だね
今からお前の名前は恵だ、いいかい、わかったら返事をするんだ!恵!!

成果物

以上

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?