Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div>
  <canvas id="myChart"></canvas>
</div>
              
            
!

CSS

              
                
              
            
!

JS

              
                var min = 0
var max = 1
var samples = 5000
var steps = 400
var step = (max-min)/steps

//f1 only
var stdDev = 60
var mean = (max+min)/2

//f2 only
var skew=1

//f3 only
var mu = 1
var sigma = 0

/*----------------------------*/

//f1
function getNormalDistribution(min, max, skew=1) {
	let u = 0, v = 0;
	while(u === 0) u = Math.random() //Converting [0,1) to (0,1)
	while(v === 0) v = Math.random()
	let num = Math.sqrt( -2.0 * Math.log( u ) ) * Math.cos( 2.0 * Math.PI * v )
	
	num = num / 10.0 + 0.5 // Translate to 0 -> 1
	if (num > 1 || num < 0) 
		num = getRandom(min, max, skew) // resample between 0 and 1 if out of range
		
	else{
		num = Math.pow(num, skew) // Skew
		num *= max - min // Stretch to fill range
		num += min // offset to min 
	}
	return num
}

//f2
function gaussian(mean, stdev) {
  var y2;
  var use_last = false;
  return function() {
    var y1;
    if (use_last) {
      y1 = y2;
      use_last = false;
    } else {
      var x1, x2, w;
      do {
        x1 = 2.0 * Math.random() - 1.0;
        x2 = 2.0 * Math.random() - 1.0;
        w = x1 * x1 + x2 * x2;
      } while (w >= 1.0);
      w = Math.sqrt((-2.0 * Math.log(w)) / w);
      y1 = x1 * w;
      y2 = x2 * w;
      use_last = true;
    }

    var retval = mean + stdev * y1;
    if (retval > 0)
      return retval;
    return -retval;
  }
}

//f3
function normal(mu, sigma, nsamples){
    if(!nsamples) nsamples = 6
    if(!sigma) sigma = 1
    if(!mu) mu=0

    var run_total = 0
    for(var i=0 ; i<nsamples ; i++){
       run_total += Math.random()
    }

    return sigma*(run_total - nsamples/2)/(nsamples/2) + mu
}

function fill(inArr, func){
	var arr = inArr
	
	let i=0
	while(i<arr.length){

		arr[i] = {
			x:step*i+min,
			y:0
		}
		i++
	}
	
	for (i=0; i<samples; i++){
		//const value = getRandom(min, max)
		const value = func()

		let j=0
		while(j<arr.length && value > arr[j].x)j++;

		if ( j != 0 && (j===arr.length || ( arr[j].x - value > value - arr[j-1].x ) ))
			arr[j-1].y++
		else
			arr[j].y++

	}
	
	return arr
}


var f1 = fill(Array(steps), ()=>getNormalDistribution(min, max, skew))
var f2 = fill(Array(steps), gaussian(mean, step*stdDev))
//var f3 = fill(Array(steps), ()=>normal(mu, sigma, samples))

const data = {
  datasets: [{
    label: 'f1',
    backgroundColor: 'rgb(255, 99, 132)',
    borderColor: 'rgb(255, 99, 132)',
    data: f1,
  },{
    label: 'f2',
    backgroundColor: 'rgb(99, 132, 255)',
    borderColor: 'rgb(99, 132, 255)',
    data: f2,
  }]
};

const config = {
  type: 'scatter',
  data: data,
  options: {}
};

const myChart = new Chart(
    document.getElementById('myChart'),
    config
  );

              
            
!
999px

Console