How does the distribution of Math.random()*50 + Math.random()*20 compare to Math.random()*70?

Jason

How does the distribution of:

var randomNumber = Math.random()*50 + Math.random()*20;

compare to that of:

var randomNumber = Math.random()*70;
Blindman67

The first will not produce a flat distribution with more values near 70/2, while the second will produce an even distribution..

The easy way to find out is just to sample the values and graph them.

Sampled slowly just for fun.

const ctx = canvas.getContext("2d");
const a1 = new Float64Array(70);
const a2 = new Float64Array(70);
var total = 0;
function doSamples(samples){
    for(var i = 0; i < samples; i ++){
        var n1 = Math.random() * 50 + Math.random() * 20;
        var n2 = Math.random() * 70;
        a1[n1 | 0] += 1;
        a2[n2 | 0] += 1;
    }
    var max = 0;
    for(i = 0; i < 70; i ++){
        max = Math.max(max,a1[i],a2[i]);
    }
    ctx.clearRect(0,0,canvas.width,canvas.height);
    for(i = 0; i < 70; i ++){
        var l1 = (a1[i] / max) * canvas.height;
        var l2 = (a2[i] / max) * canvas.height;
        ctx.fillStyle = "Blue";
        ctx.fillRect(i * 8,canvas.height - l1,4,l1)
        ctx.fillStyle = "Orange";
        ctx.fillRect(i * 8 + 4,canvas.height - l2,4,l2)
        
    }
    total += samples;
    count.textContent = total;
}
function doit(){
    doSamples(500);
    setTimeout(doit,100);
}
doit();
canvas {border:2px solid black;}
<canvas id="canvas" width = 560 height =  200></canvas><br>
Orange is random() * 70<br>
Blue is random() * 50 + random() * 20<br>
Graph is normalised.
<span id="count"></span> samples.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related