random.mjs 351 B

12345678910111213141516171819
  1. // Super quick and dirty LCG PRNG
  2. const m = 0xffffffff
  3. let X = Math.floor(Math.random() * (m - 1))
  4. const a = 16807
  5. const c = 0
  6. // Should probably be a large-ish number
  7. export function seed(i) {
  8. if (i < 0) {
  9. throw new Error('Seed must be a positive integer')
  10. }
  11. X = i & m
  12. }
  13. export function random() {
  14. X = (a * X + c) % m
  15. return X / m
  16. }