|
| 1 | +<html> |
| 2 | +<body> |
| 3 | + <script type="text/javascript"> |
| 4 | + |
| 5 | + const HEIGHT = 250; |
| 6 | + const WIDTH = 250; |
| 7 | + const ITERS = 250000; |
| 8 | + const AREA = HEIGHT * WIDTH; |
| 9 | + |
| 10 | + function cryptoRandom() { |
| 11 | + // Divide a random UInt32 by the maximum value (2^32 -1) to get a result between 0 and 1 |
| 12 | + return window.crypto.getRandomValues(new Uint32Array(1))[0] / 4294967295; |
| 13 | + } |
| 14 | + |
| 15 | + function randomCoords(rnd) { |
| 16 | + var num = Math.floor(rnd() * AREA); |
| 17 | + var xx = Math.floor(num / WIDTH); |
| 18 | + var yy = num % HEIGHT; |
| 19 | + return [xx, yy]; |
| 20 | + } |
| 21 | + |
| 22 | + function doRender(id, rnd) { |
| 23 | + var h1 = document.createElement('h1'); |
| 24 | + h1.innerHTML = id; |
| 25 | + document.body.appendChild(h1); |
| 26 | + var canvas = document.createElement('canvas'); |
| 27 | + document.body.appendChild(canvas); |
| 28 | + var ctx = canvas.getContext('2d'); |
| 29 | + canvas.setAttribute('height', HEIGHT); |
| 30 | + canvas.setAttribute('width', WIDTH); |
| 31 | + |
| 32 | + var xx, yy, ii; |
| 33 | + |
| 34 | + for (var ii = 0; ii < ITERS; ii++) { |
| 35 | + |
| 36 | + [xx, yy] = randomCoords(rnd); |
| 37 | + ctx.fillStyle = 'rgba(0, 0, 0, 0.1)'; |
| 38 | + ctx.fillRect(xx, yy, 1, 1); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + doRender('mathRandom', Math.random); |
| 43 | + doRender('cryptoRandom', cryptoRandom); |
| 44 | + </script> |
| 45 | +</body> |
| 46 | +</html> |
0 commit comments