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

              
                <!-- 
  I'm using this to detect if this
  is the showcase pen. Other pens
  might use the SmallPRNG library
  in the future, then there should
  be no jQuery stuff.
-->
<div id="main-pen"></div>

<div id="container">
  <h1>SmallPRNG - Pseudo-random number generator</h1>
  <hr/>
  <div class="column">
    <h2>Input - seeds and such</h2>
    <hr />
    <div class="field">
      <label>Seed</label>
      <input type="number" name="seed" value="1337" />
      <input type="button" name="time" value="Use time" />
    </div>
    
    <div class="field">
      <label>Min/max</label>
      <input class="half" type="number" name="min" value="1" />
      <input class="half" type="number" name="max" value="100" />
    </div>
    
    <div class="field go">
      <input type="button" name="rval" value="randval()" />
      <input type="button" name="rmax" value="max" />
      <input type="button" name="rmm" value="min, max" />
    </div>
    
  </div>
  <div class="column">
    <h2>Output - result and context</h2>
    <hr />
    <table>
      <tr><td>result:</td><td id="rr">Click a button!</td></tr>
      <tr><td>rctx.a:</td><td id="ra"></td></tr>
      <tr><td>rctx.b:</td><td id="rb"></td></tr>
      <tr><td>rctx.c:</td><td id="rc"></td></tr>
      <tr><td>rctx.d:</td><td id="rd"></td></tr>
      <tr><td>rctx.s:</td><td><span id="rs">0</span> (steps)</td></tr>
    </table>
    <input type="button" name="step" value="Step CTX" id="stepper" />
  </div>
  <div class="clear"></div>
  <div id="footer">
    MIT licensed, use wherever. A link back to my CodePen profile or imagine-programming.com would be appreciated. Also check out <a href="http://burtleburtle.net/bob" target="_blank">Robert J. Jenkins Jr's website</a>, the inventor of this algorithm. I also made a pen which displays an ISAAC CPRNG cipher. <a href="https://codepen.io/ImagineProgramming/pen/CIseE/" target="_blank">Check it out!</a>
  </div>
</div>
              
            
!

CSS

              
                @mixin prefix($option, $value) {
  -webkit-#{$option}: $value;
   -khtml-#{$option}: $value;
     -moz-#{$option}: $value;
      -ms-#{$option}: $value;
       -o-#{$option}: $value;
          #{$option}: $value;
}

@mixin center($position) {
  position: $position;
  left: 50%;
  top: 50%;
  @include prefix(transform, translate(-50%, -50%));
}

%clearfix {
  *zoom: 1;
  &:before, &:after {
    content: " ";
    display: table;
  }
  &:after {
    clear: both;
  }
}

*, *:before, *:after {	
  @include prefix(box-sizing, border-box);
}

body, html {
	margin: 0;
	padding: 0;
}

body {
  background: #333333;
  font-family: "Roboto", "Helvetica", sans-serif;
  font-weight: 300;
  padding: 0 10px;
}

#container {
  padding: 20px;
  background: #454545;
  width: 600px;
  color: #ffffff;
  @include center(absolute);
  @include prefix(border-radius, 5px);
  @include prefix(box-shadow, 0px 7px 26px 0px rgba(30, 30, 30, 0.55));
  
  @extend %clearfix;
  
  .clear {
    @extend %clearfix;
  }

  h1, h2 {
    margin: 0;
    padding: 0;
  }
  
  h1 {
    font-size: 1.5em;
  }
  
  h2 {
    font-size: 1.0em;
  }
  
  hr {
	border: 0;
	height: 0;
	border-top: 1px solid rgba(0, 0, 0, 0.1);
	border-bottom: 1px solid rgba(255, 255, 255, 0.3);
  }
  
  div.column {
    float: left;
    width: 49%;
    
    background: #333;
    @include prefix(border-radius, 5px);
    @include prefix(box-shadow, 0px 7px 26px 0px rgba(30, 30, 30, 0.55));
    border: #444 1px solid;
    
    padding: 5px;
    
    &:first-of-type {
      margin-right: 2%;
    }
  }
  
  label {
    display: block;
    font-size: 11pt;
  }
  
  #stepper {
    width: 100%;
    margin-top: 5px;
  }
  
  input {
    background: #333;
    @include prefix(border-radius, 5px);
    @include prefix(box-shadow, 0px 7px 26px 0px rgba(30, 30, 30, 0.55));
    border: #444 1px solid;
    color: #fff;
    
    padding: 3px;
    margin-bottom: 10px;
    min-height: 30px;
    
    &:focus {
      outline: 0;
    }
    
    &:hover {
      border: #888 1px solid;
      background: #444;
    }
    
    &:active {
      border: #fff 1px solid;
    }
    
    &[type="number"] {
      width: 70%;
      
      &.half {
        width: 49%;
      }
    }
    
    &[type="button"] {
      width: 28%;
    }
  }

  div.go input[type="button"] {
    width: 32%;
  }
  
  &:after {
    clear: both;
    content: '';
  }
  
  table {
    width: 100%;
    border-spacing: 0px;
    border-collapse: separate;
    
    tr td:first-of-type {
      width: 100px;
    }
  }
  
  a {
    $color: #a9a9a9;
    color: $color;
    text-decoration: none;
    &:hover, &:active, &:focus {
      color: lighten($color, 20%);
    }
  }
  
  #footer {
    color: #898989;
    font-size: 10pt;
    padding-top: 15px;
  }
}
              
            
!

JS

              
                /**
  SmallPRNG library for JavaScript. Not special, 
  not optimized, not perfect. It produces pseudo-
  random numbers by manipulating a context which 
  was initialized with a seed. 
  
  MIT licensed, use wherever you like, but a link
  back to my CodePen (ImagineProgramming) or website
  is appreciated (http://www.imagine-programming.com)
  
  Based on:
  http://burtleburtle.net/bob/rand/smallprng.html
*/

(function(target, name) {
  // maximum randval for .random([max/min [, max]])
  var RMAX = 0x7FFFFFFF;
  
  var floor = Math.floor,
      abs = Math.abs;
   
  // 32-bits rotate
  var rot = function(x, k) {
    return (((x << k) & 0xffffffff) | (x >>> (32 - k)))>>>0;
  };
  
  // constructor for the context
  var SmallPRNG = function(seed) {
    this.a = 0xf1ea5eed;
    this.b = seed;
    this.c = seed;
    this.d = seed;
    this.s = 0;
  };
  
  // reseed the context
  SmallPRNG.prototype.seed = function(seed) {
    this.a = 0xf1ea5eed;
    this.b = seed;
    this.c = seed;
    this.d = seed;
    this.s = 0;
  };
  
  // reseed the context b, c and d fields
  SmallPRNG.prototype.seedAll = function(b, c, d) {
    this.a = 0xf1ea5eed;
    this.b = b;
    this.c = c;
    this.d = d;
    this.s = 0;
  };
  
  // get the next randval in the ctx
  SmallPRNG.prototype.randval = function() {
    // notice the >>>0, which is to get an unsigned integer.
    var e  = ((this.a - rot(this.b, 27)) & 0xffffffff)>>>0;
    this.a = ((this.b ^ rot(this.c, 17)) & 0xffffffff)>>>0;
    this.b = ((this.c + this.d) & 0xffffffff)>>>0;
    this.c = ((this.d + e) & 0xffffffff)>>>0;
    this.d = ((e + this.a) & 0xffffffff)>>>0;
    this.s++;
    return this.d;
  };
  
  // step `times' times in the CTX
  SmallPRNG.prototype.step = function(times) {
    times = (typeof(times) === "number" ? times : 1);
    if(times === 0) {
      times = 1;
    }
    
    for(var i = 0; i < times; i++) {
      var e  = ((this.a - rot(this.b, 27)) & 0xffffffff)>>>0;
      this.a = ((this.b ^ rot(this.c, 17)) & 0xffffffff)>>>0;
      this.b = ((this.c + this.d) & 0xffffffff)>>>0;
      this.c = ((this.d + e) & 0xffffffff)>>>0;
      this.d = ((e + this.a) & 0xffffffff)>>>0;
      this.s++;
    }
  };
  
  SmallPRNG.prototype.random = function() {
    var r = ((this.randval() % RMAX) / RMAX);
    switch(arguments.length) {
      // zero arguments, return the 0-1 random factor
      case 0: {
        return r;
      } break;
        
      // 1 argument (max val), return random between 1 and max
      case 1: {
        var u = arguments[0];
        if(u < 1) {
          console.log("upper limit invalid");
          return null;
        }
        
        return (floor(r * u) + 1);
        
      } break;
        
      // 2 arguments (min, max val), return random between min and max
      case 2: {
        var l = arguments[0];
        var u = arguments[1];
        
        if(l >= u) {
          console.log("upper limit invalid");
          return null;
        }
        
        return (floor(r * (u - l + 1)) + l);
      } break;
        
      default: {
        console.log("invalid amount of arguments");
      } break;
    }
    
    return null;
  };
  
  target[name] = SmallPRNG;
}(window, "SmallPRNG"));

/*
  jQuery was only used to set up a quick
  interface. Don't be mad :(
  
  This code is not required, use the lib
  above if you really really want to use
  this junk.
  
  mainPen is used to detect the showcase pen,
  future pens might refer to this one and they
  should not execute this code.
*/

var mainPen = document.getElementById("main-pen");
if(mainPen) {
  $(function() {
    var rctx = new SmallPRNG(1337);
    var $seedField = $('input[name="seed"]');
    var $timeSeed = $('input[name="time"]');
    var $min = $('input[name="min"]');
    var $max = $('input[name="max"]');
    var $rval = $('input[name="rval"]');
    var $rmax = $('input[name="rmax"]');
    var $rmm = $('input[name="rmm"]');
    var $stp = $('#stepper');

    $timeSeed.click(function() {
      var time = (new Date()).getTime();
      $seedField.val(time);
      rctx = new SmallPRNG(+time);
      return false;
    });

    $seedField.change(function() {
      rctx = new SmallPRNG(+$(this).val());
      return false;
    });

    var dumpCTX = function(r) {
      if(r) {
        $("#rr").text(r);
      }
      $("#ra").text(rctx.a);
      $("#rb").text(rctx.b);
      $("#rc").text(rctx.c);
      $("#rd").text(rctx.d);
      $("#rs").text(rctx.s);
    };

    $rval.click(function() {
      var r = rctx.randval();
      dumpCTX(r);
      return false;
    });

    $rmax.click(function() {
      var max = +$max.val();
      var r = rctx.random(max);
      dumpCTX(r);
      return false;
    });

    $rmm.click(function() {
      var min = +$min.val();
      var max = +$max.val();
      var r = rctx.random(min, max);
      dumpCTX(r);
      return false;
    });

    $stp.click(function() {
      rctx.step();
      dumpCTX();
      return false;
    });

    $rval.click();
  });  
}
              
            
!
999px

Console