Login
7 branches 0 tags
Ben (X13/Arch) Division now always returns a float 7f852a9 3 years ago 864 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 [bit-xor [time] [time/milliseconds]]]]

[defn random/rng! []
      [set! random/seed [+ 12345 [* random/seed 1103515245]]]
      [bit-or [bit-shift-left [bit-and random/seed #xFFFF] 16]
              [bit-and [bit-shift-right 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]
          [rem [abs [random/rng!]] max]
          [random/rng!]]]

[random/seed-initialize!]