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?

More than 3 years have passed since last update.

予測困難な簡易乱数

Last updated at Posted at 2022-02-13

単純に、物理状態(日時、タイマー、パフォーマンス、CPU温度、ユーザー操作に依存するものなど)を取得する関数を使います。この方法の欠点は、再現性の低いバグの原因となって、デバッグが難しくなる場合があることです。

以下では window.performance.now() の値を使って、Math.random を数回更新した後の値を使用します。この場合、出てきた順番から次の候補(4個)を予測することまでは可能かもしれませんが、一つに絞り込むことは困難なハズです。

動作確認は Google Chrome バージョン: 98.0.4758.80(Official Build)(x86_64)で行っています。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>乱数テスト</title>
  </head>
  <body>
    <h1>乱数テスト</h1>
    <p><label><input type="button" onclick="doClick();" value="次"></label> : Math.random() = <span id="value"></p>

    <script type="text/javascript">
    <!--

      function doClick() {
          let n = 0;
          for (const c of window.performance.now().toString())
              n += c.charCodeAt(0);
          n &= 3;
          // ここでは Math.random() を使用してますが
          // もっと簡易な乱数を想定しています。
          for (let s = 0; s < n; s++)
              Math.random();
          document.getElementById('value').innerText = Math.trunc(Math.random() * 100);
      }

      doClick();

      -->
    </script>
  </body>
</html>
0
0
23

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?