Module 12 · Statistical Computing and Reproducibility Lesson 116 of 120

Pseudorandom Numbers, Seeds, and Reproducibility

Replaying a simulation without confusing reproducibility with security.

2:33 clip6:14:40–6:17:13 in the full courseWatch on YouTube

Transcript

16 sentences · select one to jump there

Check your understanding

Is this generator appropriate for secure password-reset tokens?

Choose one answer

Code lab

Run it yourself

The lesson source in 7 languages. Edit it, run TypeScript and Python right here, and compare with the expected output.

116-pseudorandom-numbers-seeds-and-reproducibility.ts
Start from GitHub
/**
 * Fintech Math Bootcamp · Lesson 116 of 120
 * Pseudorandom Numbers, Seeds, and Reproducibility
 * Module 12: Statistical Computing and Reproducibility
 *
 * Scenario: Replaying a simulation without confusing reproducibility with security
 * Rule:     stateₜ₊₁=(1664525·stateₜ+1013904223) mod 2³²
 *
 * Try it:   Is this generator appropriate for secure password-reset tokens?
 *
 * Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/statistical-computing-and-reproducibility/pseudorandom-numbers-seeds-and-reproducibility/
 * Free course:    https://courses.thefintechbuilder.com
 * Synthetic teaching example, not financial advice or a production library.
 */

export function lesson116() {
  let state=42;
  const next=()=>{state=(Math.imul(1664525,state)+1013904223)>>>0;
    return state/4294967296;};
  const draws=[next(),next(),next()];
  const result={draws,finalState:state};
  return result;
}

export const checkedResult = {"draws":[0.2523451747838408,0.08812504541128874,0.5772811982315034],"finalState":2479403867};

// Run this file directly: npx tsx lessons/12-statistical-computing-and-reproducibility/116-pseudorandom-numbers-seeds-and-reproducibility.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
  console.log(JSON.stringify(lesson116(), null, 2));
}

Your output

Press Run to execute the code in your browser.

Expected output

{
  "draws": [
    0.2523451747838408,
    0.08812504541128874,
    0.5772811982315034
  ],
  "finalState": 2479403867
}

Prefer your own machine? Every file is in the course repository · open it in Codespaces.

Lesson notes

The rule

stateₜ₊₁=(1664525·stateₜ+1013904223) mod 2³²