Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

              
                <html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Gaussian Random</title>

	<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>

	<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>

</head>
<body>

	<h1>Gaussian distribution</h1>
  
   <button id="generate-gaussian">Generate new set</button>
  
	<div id="gaussian-distribution-graph"></div>
	
</body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                $(document).ready( function() {

  var array2 = [],
		  length = 1e5;

  function gaussianRandom() {

	 		var r1, r2, w, X1, X2;

	 		do {
	 		  r1 = 2 * Math.random() - 1;
	 			r2 = 2 * Math.random() - 1;
	 			w = r1 * r1 + r2 * r2;
	 		} while ( w >= 1 );

	 		w = Math.sqrt( ( -2 * Math.log( w ) ) / w );

	 		X1 = r1 * w;
	 		X2 = r2 * w;

      return X1;

}

	function createGaussianRandomArray() {
		// reset the array
		array2 = [];

		// fill the array with 'length' elements generated        by Math.random() function
		for ( var i = 0; i < length; i++ ) {
			array2.push( gaussianRandom() );
		}
	}
  
  function normalize( array ) {
		
		var min = Math.min.apply( null, array ),
			max = Math.max.apply( null, array );
    
    var boundary = Math.max( -min, max );
    min = -boundary;
    max = boundary;

		var new_array = [];

		for ( var i = 0; i < array.length; i++ ) {
			new_array.push( ( array[i] - min ) / ( max - min ) );
		}

		return new_array;
	}
  
  function gaussianDistributionExecution() {
    // create the array
    createGaussianRandomArray();
    
    var normalized_array = normalize( array2 );
    
    // create density data for graph
    var density_data = {
	    x: normalized_array,
	    name: 'x density',
	    type: 'histogram'
	  };
    
    // plot the density distribution using Plotly
    Plotly.newPlot('gaussian-distribution-graph', [ density_data ] );
    
  }
  
  // Generate new set of values and plot it when button is clicked
	$("#generate-gaussian").click( function() {
		gaussianDistributionExecution()
	});

	// on initial page's load, generate and plots a first set of data
  gaussianDistributionExecution();

});
              
            
!
999px

Console