mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
88 lines
1.9 KiB
Plaintext
88 lines
1.9 KiB
Plaintext
(defmodule Random
|
|
|
|
(def a 69069.0)
|
|
(hidden a)
|
|
(private a)
|
|
(def c 1.0)
|
|
(hidden c)
|
|
(private c)
|
|
(def m 4294967296.0)
|
|
(hidden m)
|
|
(private m)
|
|
(def s 19380110.0)
|
|
(hidden s)
|
|
(private s)
|
|
|
|
(doc seed "seed resets the seed of the random number generator.")
|
|
(defn seed []
|
|
(set! s (Double.from-long (System.nanotime))))
|
|
|
|
(doc seed-from "seed-from resets the seed of the random number generator to `new-seed`.")
|
|
(defn seed-from [new-seed]
|
|
(set! s new-seed))
|
|
|
|
(doc random "random returns a float from 0 to 1.")
|
|
(defn random []
|
|
(do
|
|
(set! s (mod (+ (* s a) c) m))
|
|
(/ s m)))
|
|
)
|
|
|
|
(defmodule Int
|
|
(defn random-between [lower upper]
|
|
(let [diff (- upper lower)]
|
|
(+ (Double.to-int (* (Double.from-int diff) (Random.random))) lower)))
|
|
|
|
(defn random []
|
|
(random-between 0 MAX))
|
|
)
|
|
|
|
(defmodule Float
|
|
(defn random-between [lower upper]
|
|
(let [diff (- upper lower)]
|
|
(+ (Double.to-float (* (Double.from-float diff) (Random.random))) lower)))
|
|
|
|
(defn random []
|
|
(random-between 0.0f (from-int Int.MAX)))
|
|
)
|
|
|
|
(defmodule Long
|
|
(defn random-between [lower upper]
|
|
(let [diff (- upper lower)]
|
|
(+ (Double.to-long (* (Double.from-long diff) (Random.random))) lower)))
|
|
|
|
(defn random []
|
|
(random-between 0l (from-int Int.MAX)))
|
|
)
|
|
|
|
(defmodule Double
|
|
(defn random-between [lower upper]
|
|
(let [diff (- upper lower)]
|
|
(+ (* diff (Random.random)) lower)))
|
|
|
|
(defn random []
|
|
(random-between 0.0 (from-int Int.MAX)))
|
|
)
|
|
|
|
(defmodule Char
|
|
(defn random []
|
|
(Char.from-int (Int.random)))
|
|
|
|
(defn random-between [a b]
|
|
(Char.from-int (Int.random-between (Char.to-int a) (Char.to-int b))))
|
|
)
|
|
|
|
(defmodule String
|
|
(defn random-sized [n]
|
|
(from-chars &(Array.repeat n &Char.random)))
|
|
)
|
|
|
|
(defmodule Byte
|
|
(defn random-between [lower upper]
|
|
(Byte.from-int (Int.random-between (Byte.to-int lower) (Byte.to-int upper))))
|
|
|
|
(defn random []
|
|
(Byte.from-int (Int.random)))
|
|
)
|
|
|