Skip to content

Instantly share code, notes, and snippets.

@peketamin
Last active November 6, 2023 13:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peketamin/c8e544cf21a05d24b763817f28b619fa to your computer and use it in GitHub Desktop.
Save peketamin/c8e544cf21a05d24b763817f28b619fa to your computer and use it in GitHub Desktop.
"10年程前からビデオゲーム開発者の間で語り継がれている謎の一様乱数生成アルゴリズム" を Python で実装 (自信なし)

"10年程前からビデオゲーム開発者の間で語り継がれている謎の一様乱数生成アルゴリズム" を Python で実装 (自信なし)

元ネタ

要点

  • 乱数生成器は乱数生成の度に状態を更新しながら進むそう
  • つまり、パラレルに計算するとランダムさが低い乱数の集合になってしまう?
    • となると、スケールしない
  • バラバラに動かしても状態依存のない、純粋に入力値のみに依存する乱数生成アルゴリズムが必要

Python 実装

import math

import numpy as np


def rand(vec2: np.matrix) -> float:
    return math.modf(  # 実数を小数部と整数部に分ける
        math.sin(
            # 資料通りに内積を取ると 2x2 の2D行列になってしまった (numpyの仕様?) ので、順序を変更した
            (vec2 @ np.matrix([[12.9898], [4.1414]]))[0, 0]  # 4.1414? 78.233? どっちだろう
            * 43758.5453  # 固定値
        )
    )[0]  # decimal part

In [110]: rand(np.matrix([0.3, 0.2]))
Out[110]: 0.9927441503341535

In [112]: rand(np.matrix([0.1, 0.8]))
Out[112]: 0.26372426998544235

この実装でいいか自信ないです。分布を調べてない (調べ方もわからない) ので、実装があっているかわかりません。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment