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

              
                <article>
  <h1>RoyalUr.net dice randomness distribution</h1>
  <hr>
  <p>
    This page throws the dice from <a href="https://royalur.net/">RoyalUr.net</a> a given number of times and shows
    how the result matches the expected distribution of randomness.
  </p>
  <form>
    <input name="iterations" value="10000000" />
    <button>Roll Dice!</button>
  </form>

  <table>
    <caption>Comparison of expected and actual distribution of <span class="iterations"></span> dice throws</caption>

    <thead>
      <tr>
        <th>Value</th>
        <th>Expected</th>
        <th>Actual</th>
        <th>Deviation</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>0</td>
        <td value="0">6.25%</td>
        <td data-value="0"></td>
        <td></td>
      </tr>
      <tr>
        <td>1</td>
        <td value="1">25.00%</td>
        <td data-value="1"></td>
        <td></td>
      </tr>
      <tr>
        <td>2</td>
        <td value="2">37.50%</td>
        <td data-value="2"></td>
        <td></td>
      </tr>
      <tr>
        <td>3</td>
        <td value="3">25.00%</td>
        <td data-value="3"></td>
        <td></td>
      </tr>
      <tr>
        <td>4</td>
        <td value="4">6.25%</td>
        <td data-value="4"></td>
        <td></td>
      </tr>
    </tbody>
  </table>
</article>
              
            
!

CSS

              
                @import url("https://fonts.googleapis.com/css2?family=Nunito:wght@200&display=swap");

a {
  color: inherit;
}

article {
  display: block;
  flex-shrink: 0;
  box-sizing: border-box;
  width: 90%;
  max-width: 90%;
  padding: 0.5em 1.5em;
  margin: 1em auto;
  font-size: 1.4em;
  background-color: #202930;
  box-shadow: 0 0 0.6em 0.3em black;
  color: white;
}

body {
  background: #202930
    url("https://royalur.net/res/wood_background.v1614055952.webp") repeat;
  background-size: 35vmin;
  font-family: Nunito, sans-serif;
  font-size: 1em;
  line-height: 1.6em;
}

button,
input {
  border-radius: 0.15em;
  padding: 0.15em 0.3em;
  margin: 0.3em;
  font-family: Nunito, sans-serif;
  font-size: 2em;
  color: white;
  cursor: pointer;
  background-color: rgba(0, 0, 0, 0.8);
  transition: background-color 0.1s ease-in-out;
}

button {
  border: 3px solid white;
}

button:hover {
  background-color: #151a1f;
  transition: background-color 0.1s ease-in-out;
}

caption {
  padding: 0.3em;
  caption-side: bottom;
  font-size: 50%;
}

form {
  text-align: center;
}

h1 {
  margin-top: 0.2em;
  margin-bottom: 0.15em;
  font-size: 1.5em;
}

input {
  text-align: right;
  max-width: 6.5em;
}

table {
  table-layout: auto;
  width: 50%;
  border-collapse: collapse;
  margin: auto;
}

td:first-child,
td:last-child {
  font-weight: bold;
}

th,
td {
  border: 1px solid white;
  padding: 0.5em;
  text-align: center;
}

tr:nth-child(odd) td {
  background-color: #151a1f;
}

.minus {
  color: red;
}
.plus {
  color: green;
}
.zero {
  color: lime;
}
.running {
  opacity: 0.35;
}

              
            
!

JS

              
                function calculateScores(iterations) {
  window.CP.PenTimer.MAX_TIME_IN_LOOP_WO_EXIT = 10000;

  const scores = {
    0: 0,
    1: 0,
    2: 0,
    3: 0,
    4: 0
  };

  for (let counter = 0; counter < iterations; counter++) {
    const diceValue = countDiceUp(generateRandomDiceValues());
    scores[diceValue]++;
  }

  return scores;
}

document.querySelector("form").addEventListener("submit", (event) => {
  event.preventDefault();

  const resultTable = document.querySelector("table");
  const iterations = parseInt(
    document.querySelector('[name="iterations"]').value,
    10
  );

  resultTable.classList.add("running");
  document.querySelector(".iterations").innerText = iterations.toLocaleString();

  const scores = calculateScores(iterations);

  const expected = {
    0: 6.25,
    1: 25.0,
    2: 37.5,
    3: 25.0,
    4: 6.25
  };

  for (const [dieNumber, count] of Object.entries(scores)) {
    const percentage = ((100 / iterations) * count).toFixed(2);
    const deviation = (expected[dieNumber] - percentage).toFixed(2);

    const element = document.querySelector(`[data-value="${dieNumber}"]`);
    const deviationElement = element.nextElementSibling;

    let className;
    if (deviation < 0) {
      className = "minus";
    } else if (deviation > 0) {
      className = "plus";
    } else {
      className = "zero";
    }

    element.innerText = `${percentage}%`;
    deviationElement.innerText = `${deviation}%`;
    deviationElement.className = className;
  }

  resultTable.classList.remove("running");
});

              
            
!
999px

Console