Login
7 branches 0 tags
Ben (X220/Parabola) Big refactoring, mostly surrounding the GC 4f57ee7 3 years ago 651 Commits
nujel / stdlib / random / random-number-generator.nuj
;; Nujel - Copyright (C) 2020-2021 - Benjamin Vincent Schulenburg
;; This project uses the MIT license, a copy should be included under /LICENSE

[def random/seed 0]

[defn random/seed-initialize! []
  [set! random/seed [logxor [time] [time/milliseconds]]]]

[defn random/rng! []
  [set! random/seed [+ 12345 [* random/seed 1103515245]]]
  [logior [ash [logand random/seed #xFFFF] 16]
          [logand [ash random/seed -16] #xFFFF]]]

[defn random/seed! [new-seed]
  "Set a new seed value for the RNG"
  [set! seed new-seed]]

[defn random/seed []
  "Return the current RNG seed value"
  seed]

[defn random [max]
  "Return a value from 0 to MAX, or, if left out, a random int"
  [if [numeric? max]
      [% [abs [random/rng!]] max]
      [random/rng!]]]

[random/seed-initialize!]