回答編集履歴

1

調整

2023/01/22 06:07

投稿

yambejp
yambejp

スコア114827

test CHANGED
@@ -1,3 +1,36 @@
1
1
  この手のアルゴリズムは偏りが発生するのは仕方ありません。
2
2
  シードを使ったランダムな1未満の数ではなく、mからnまでの整数を返す仕組みにすれば
3
3
  偏りのある戻り値の剰余を利用して比較的安定した乱数を発生させられます
4
+
5
+ # 追記
6
+ 範囲を指定してランダム数値を生成
7
+ 以下サンプルは10~99の数値を100個抽出、seed指定なし
8
+ ```javascript
9
+ class Random {
10
+ constructor(seed = 88675123) {
11
+ this.x = 123456789;
12
+ this.y = 362436069;
13
+ this.z = 521288629;
14
+ this.w = seed;
15
+ }
16
+ next() {
17
+ let t= this.x ^ (this.x << 11);
18
+ this.x = this.y;
19
+ this.y = this.z;
20
+ this.z = this.w;
21
+ return this.w = (this.w ^ (this.w >>> 19)) ^ (t ^ (t >>> 8));
22
+ }
23
+ range(min, max) {
24
+ return min + (Math.abs(this.next()) % (max + 1 - min));
25
+ }
26
+ }
27
+ const r= new Random();
28
+ const datas=[];
29
+ const min=10;
30
+ const max=99;
31
+ for(let i = 0; i < 100; i++) {
32
+ datas.push(r.range(min,max));
33
+ }
34
+ console.log(datas);
35
+
36
+ ```