English version is below. 線形合同法っていうのを使って乱数を生成しています。 Scratchの乱数は簡単にハックできるという情報を見て自作乱数を作ってみました。 https://scratch.mit.edu/discuss/post/8740776/ このプロジェクトでは、まずseedを設定して線形合同法っていう乱数の式に再代入を繰り返して乱数を生成しています。ループする(同じ乱数が出てくる)とプロジェクトは止まります。というか、乱数上限が1万なので、最大でも1万回で止まります。後で紹介する Hull-Dobell条件 のおかげで常に最大の1万回でループを作れるらしいです。 計算式 線形合同法の式をそのまま入れただけです。 (a * seed + c) mod m ただし、このプロジェクトでは seed→乱数 m→乱数上限 です。 これでハックが難しくなったのかはわかりませんが、自分でハックしてみようとしてもできませんでした(JS 3年もやってないから弱いだけかもしれない)。 ちなみに、Scratchの乱数はぼくでも簡単にハックできました。乱数に依存しているオンラインゲームとかあったら注意が必要なのかもしれません。 あと、a,cは乱数上限が10000のときにうまくいくように設定したので乱数上限変えるとa,cも変えないとうまくいかないと思います。 詳しくは Hull-Dobell条件 と検索してください。 自分でもわからない部分がありますが、なにか質問あったらコメントへどうぞ。 Just a heads-up: I only studied English in Japanese high school, so my English may sound a little odd sometimes. I’m generating random numbers using a Linear Congruential Generator (LCG). Scratch’s built-in random numbers are easy to predict, so I decided to make my own. https://scratch.mit.edu/discuss/post/8740776/ (Japanese) In this project, I first set a seed and then repeatedly apply the LCG formula to generate random numbers. When the sequence cycles (i.e., repeats), the project stops. Since the modulus (upper limit) is 10,000, the project will always stop within at most 10,000 steps. Thanks to the Hull–Dobell theorem, with the right parameters, it’s possible to achieve the full period of 10,000. The formula is: (a * seed + c) mod m In this project: seed → the previous random number (used again as input) m → the maximum value, i.e., the modulus I’m not sure whether this LCG implementation is actually much harder to crack than Scratch’s default random function—maybe I just lack experience with JS. As a side note, I was able to predict Scratch’s built-in random numbers very easily. So if an online game relies on Scratch’s randomness, that could be a potential security risk. Also, note that the parameters a and c were chosen to work well when the upper limit m is 10,000. If you change the upper limit, you’ll need to adjust a and c to satisfy the Hull–Dobell conditions. (Search for “Hull–Dobell theorem” for more details.) I don’t fully understand every part myself, so if you have any questions, feel free to leave a comment!
このプロジェクトは CC BY-SA 2.0 でライセンスされています。2026年1月22日以降(例外あり)に投稿された作品は独自ライセンスになります。詳しくは https://mitscratch.freshdesk.com/en/support/solutions/articles/4000219182-scratch-terms-of-service