Skip to content

Commit ded7335

Browse files
committed
test01, test02, console framework
1 parent 0f622c3 commit ded7335

9 files changed

+202
-1
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/*
2+
org/*

.htaccess

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
AddDefaultCharset utf-8
2+
DirectoryIndex index.html

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
# js-random-test
1+
## js-random-test
2+
JS Math.random() test

index.html

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>JS Math.random() tests</title>
5+
<link rel="stylesheet" href="js/console.css">
6+
</head>
7+
<h4>JS Math.random() tests</h4>
8+
<ul>
9+
<li><a href="test01.html" target="_blank">test 01</a></li>
10+
<li><a href="test02.html" target="_blank">test 02</a></li>
11+
</ul>
12+
</html>

js/cDistribution.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class cDistribution {
2+
constructor(limit) {
3+
this.limit = limit;
4+
this.data = Array(this.limit).fill(0).map(v => 0);
5+
// console.log('Distribution: data:', this.data);
6+
}
7+
8+
reset() {
9+
for (let i = 0; i < this.limit; i++) {
10+
this.data[i] = 0;
11+
}
12+
}
13+
14+
reg(value) {
15+
if (!Number.isInteger(value) || value < 0 || this.limit <= 0) {
16+
return;
17+
}
18+
this.data[value] += 1;
19+
}
20+
21+
stats() {
22+
let count = 0;
23+
let sum = 0;
24+
let max = 0;
25+
this.data.forEach((v, i) => {
26+
count += v;
27+
sum += v * i;
28+
max = Math.max(max, v)
29+
});
30+
const avg = count ? sum / count : 0;
31+
return {count, sum, max, avg}
32+
}
33+
34+
map() {
35+
return this.data.map(v => v > 9 ? '+' : v).join('').replace(/0/g, '.')
36+
}
37+
}

js/console.css

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
body {
2+
background: #ddd;
3+
}
4+
body, textarea {
5+
font-size: 16px;
6+
}
7+
a {
8+
text-decoration: none;
9+
}
10+
#app {
11+
max-width: 1024px;
12+
margin: 0 auto;
13+
}
14+
#log {
15+
background: #eee;
16+
width: 100%;
17+
height: 400px;
18+
}

js/console.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const elem = (id) => document.getElementById(id);
2+
const log = elem('log');
3+
4+
const putlines = (aa) => {
5+
log.value = aa.join('\n');
6+
};
7+
8+
// ---
9+
10+
const formatNumber = (value, places) => {
11+
const len = value.toString().length;
12+
const spaces = Math.max(0, places - len);
13+
return `${' '.repeat(spaces)}${value}`;
14+
};
15+
16+
const formatDict = (dict) => {
17+
const aa = [];
18+
for (const k in dict) {
19+
const value = dict[k];
20+
let repr = typeof value === 'string' ? `"${value}"` : value;
21+
aa.push(`${k}: ${repr}`);
22+
}
23+
return `{${aa.join(', ')}}`;
24+
};

test01.html

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>JS Math.random() test 01</title>
5+
<script src="js/cDistribution.js"></script>
6+
<link rel="stylesheet" href="js/console.css">
7+
</head>
8+
<div id="app">
9+
<textarea id="log"></textarea>
10+
</div>
11+
<script src="js/console.js"></script>
12+
<script>
13+
// go!
14+
const limit = 10;
15+
const n = 1000;
16+
const steps = 10;
17+
18+
const makeStep = (dist, dist_all) => {
19+
for (let i = 0; i < n; i++) {
20+
const v = Math.floor(Math.random() * limit);
21+
dist.reg(v);
22+
dist_all.reg(v);
23+
}
24+
const stats = dist.stats();
25+
return stats;
26+
};
27+
28+
const dist = new cDistribution(limit);
29+
const dist_all = new cDistribution(limit);
30+
const aa = [];
31+
for (let step = 1; step < steps+1; step++) {
32+
dist.reset();
33+
const stats = makeStep(dist, dist_all);
34+
aa.push(`step ${step}: ${formatDict(stats)}`)
35+
}
36+
const stats = dist_all.stats();
37+
aa.push(`dict all: ${formatDict(stats)}`);
38+
39+
putlines(aa);
40+
41+
</script>
42+
</html>

test02.html

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>JS Math.random() test 02</title>
5+
<script src="js/cDistribution.js"></script>
6+
<link rel="stylesheet" href="js/console.css">
7+
</head>
8+
<div id="app">
9+
<textarea id="log"></textarea>
10+
</div>
11+
<script src="js/console.js"></script>
12+
<script>
13+
// go!
14+
const limit = 100;
15+
const n = 10;
16+
const steps = 1000;
17+
let noCollisionsCnt = 0;
18+
19+
const hasCollision = (s) => {
20+
const set = new Set(s.split(''));
21+
return set.size > 2
22+
};
23+
24+
const makeStep = (dist, dist_all) => {
25+
for (let i = 0; i < n; i++) {
26+
const v = Math.floor(Math.random() * limit);
27+
dist.reg(v);
28+
dist_all.reg(v);
29+
}
30+
const stats = dist.map();
31+
if (!hasCollision(stats)) {
32+
noCollisionsCnt += 1;
33+
}
34+
// console.log(stats);
35+
return stats;
36+
};
37+
38+
const dist = new cDistribution(limit);
39+
const dist_all = new cDistribution(limit);
40+
41+
const aa = [];
42+
for (let step = 0; step < steps; step++) {
43+
dist.reset();
44+
const map = makeStep(dist, dist_all);
45+
aa.push(`${formatNumber(step, 3)}: ${map}`);
46+
}
47+
console.log('dist all:');
48+
const stats = dist_all.stats();
49+
console.log(stats);
50+
51+
const bb = [];
52+
bb.push(`noCollisions: ${noCollisionsCnt}/${steps}`);
53+
console.log(`noCollisions: ${noCollisionsCnt}/${steps}`);
54+
const k = 0.99*0.98*0.97*0.96*0.95*0.94*0.93*0.92*0.91;
55+
const noCollisionsTheory = Math.round(k * 1000);
56+
bb.push(`in Theory: ${noCollisionsTheory}/${steps}`);
57+
console.log(`in Theory: ${noCollisionsTheory}/${steps}`);
58+
bb.push('---');
59+
60+
putlines(bb.concat(aa));
61+
62+
</script>
63+
</html>

0 commit comments

Comments
 (0)