mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
core: added random to vector and double; added random and random-between interfaces
This commit is contained in:
parent
cd9a0ddbbc
commit
b561713add
@ -13,6 +13,8 @@
|
||||
(register from-int (Fn [Int] Double))
|
||||
(register to-float (Fn [Double] Float))
|
||||
(register from-float (Fn [Float] Double))
|
||||
(register random (Fn [] Double))
|
||||
(register random-between (Fn [Double Double] Double))
|
||||
(register sin (Fn [Double] Double))
|
||||
(register cos (Fn [Double] Double))
|
||||
(register tan (Fn [Double] Double))
|
||||
|
@ -7,6 +7,7 @@
|
||||
(register / (Fn [Float Float] Float))
|
||||
(register to-int (Fn [Float] Int))
|
||||
(register from-int (Fn [Int] Float))
|
||||
(register random (λ [] Float))
|
||||
(register random-between (λ [Float Float] Float))
|
||||
(register str (Fn [Float] String))
|
||||
(register copy (Fn [(Ref Float)] Float))
|
||||
|
@ -19,12 +19,13 @@
|
||||
;; >=
|
||||
;; (definterface inc (λ [a] a))
|
||||
;; (definterface dec (λ [a] a))
|
||||
;; random
|
||||
;; random-between
|
||||
;; (definterface max (λ [a a] a))
|
||||
;; (definterface min (λ [a a] a))
|
||||
;;(definterface from-string (λ [a] String))
|
||||
|
||||
(definterface random (Fn [] a))
|
||||
(definterface random-between (Fn [a a] a))
|
||||
|
||||
(definterface pi a)
|
||||
|
||||
;; These interfaces are defined internally:
|
||||
|
@ -22,6 +22,9 @@
|
||||
(defn zero []
|
||||
(V2.init 0.0 0.0))
|
||||
|
||||
(defn random []
|
||||
(V2.init (random-between 0.0 1.0) (random-between 0.0 1.0)))
|
||||
|
||||
(defn add [a b]
|
||||
(V2.init (+ (V2.x a) (V2.x b))
|
||||
(+ (V2.y a) (V2.y b))))
|
||||
@ -110,6 +113,9 @@
|
||||
(defn zero []
|
||||
(V3.init 0.0 0.0 0.0))
|
||||
|
||||
(defn random []
|
||||
(V3.init (random-between 0.0 1.0) (random-between 0.0 1.0) (random-between 0.0 1.0)))
|
||||
|
||||
(defn = [a b]
|
||||
(and (Double.= (V3.x a) (V3.x b))
|
||||
(and (Double.= (V3.y a) (V3.y b))
|
||||
@ -196,6 +202,13 @@
|
||||
(let [z 0.0]
|
||||
(VN.init n (Array.replicate n &z))))
|
||||
|
||||
(defn unit-random []
|
||||
(random-between 0.0 1.0))
|
||||
|
||||
(defn random-sized [n]
|
||||
(VN.init n (Array.repeat n unit-random)))
|
||||
|
||||
|
||||
(defn to-string [o]
|
||||
(string-join @"VectorN(dim=" (str (VN.n o)) @", vals=" (str (VN.v o))
|
||||
@")"))
|
||||
|
14
core/core.h
14
core/core.h
@ -431,6 +431,16 @@ string Double_str(double x) {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
double Double_random() {
|
||||
return rand();
|
||||
}
|
||||
|
||||
double Double_random_MINUS_between(double lower, double upper) {
|
||||
float diff = upper - lower;
|
||||
float r = ((float)(rand() % INT_MAX)) / ((float)INT_MAX);
|
||||
return lower + diff * r;
|
||||
}
|
||||
|
||||
int Float_to_MINUS_int(float x) {
|
||||
return (int)x;
|
||||
}
|
||||
@ -439,6 +449,10 @@ float Float_from_MINUS_int(int x) {
|
||||
return (float)x;
|
||||
}
|
||||
|
||||
float Float_random() {
|
||||
return rand();
|
||||
}
|
||||
|
||||
float Float_random_MINUS_between(float lower, float upper) {
|
||||
float diff = upper - lower;
|
||||
float r = ((float)(rand() % INT_MAX)) / ((float)INT_MAX);
|
||||
|
Loading…
Reference in New Issue
Block a user