LoginSignup
5
1

More than 5 years have passed since last update.

赤い💎を🍺にしたい

Last updated at Posted at 2017-09-09

ソース

<?php
$beer = "ビール";
$ruby = "ルビー";
$num = 10000;
for($j=0;$j<100;$j++) {
    $count = 0;
    for ($i = 0; $i < $num; $i++) {
        if (mt_str_shuffle($ruby) === $beer) $count++;
    }

    echo $count / $num;
    echo "\n";
}

/**
 * @param  string $str  入力された文字列を
 * @return string $str2 シャッフルして返す
 */
function mt_str_shuffle(string $str):string{
    $num = mb_strlen($str);
    $arr = array_fill(0,$num,false);
    $str2 = "";
    for($i=0;$i<$num;$i++){
        do{
            $rand = mt_rand(0,$num-1);
        }while($arr[$rand]);
        $str2 .= mb_substr($str,$rand,1);
        $arr[$rand] = true;
    }
    return $str2;
}

結果

ruby.png

結論

🍺飲みたい

5
1
2

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
1