FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge pull request #235 from namannimmo10/random · rajpratyush/lpython@67c1bd3 · GitHub

Commit 67c1bd3

Browse files
authored
Merge pull request lcompilers#235 from namannimmo10/random
Further extend the random module
2 parents 59f6d67 + e9149a8 commit 67c1bd3

4 files changed

Lines changed: 79 additions & 9 deletions

File tree

‎integration_tests/test_random.py‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,41 @@ def test_randint():
3030
print(ri2)
3131
assert ri2 >= -50 and ri2 <= 76
3232

33+
def test_uniform():
34+
r: f64
35+
r = random.uniform(5., 76.)
36+
print(r)
37+
r = random.uniform(-50., 76.)
38+
print(r)
39+
3340
def test_paretovariate():
3441
r: f64
3542
r = random.paretovariate(2.0)
3643
print(r)
3744
r = random.paretovariate(-5.6)
3845
print(r)
3946

40-
test_random()
41-
test_randrange()
42-
test_randint()
43-
test_paretovariate()
47+
def test_expovariate():
48+
r: f64
49+
r = random.expovariate(2.0)
50+
print(r)
51+
r = random.expovariate(-5.6)
52+
print(r)
53+
54+
def test_weibullvariate():
55+
r: f64
56+
r = random.weibullvariate(2.0, 3.0)
57+
print(r)
58+
r = random.weibullvariate(-5.6, 1.2)
59+
print(r)
60+
61+
def check():
62+
test_random()
63+
test_randrange()
64+
test_randint()
65+
test_uniform()
66+
test_paretovariate()
67+
test_expovariate()
68+
test_weibullvariate()
69+
70+
check()

‎src/runtime/impure/lfortran_intrinsics.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ LFORTRAN_API void _lfortran_random_number(int n, double *v)
3030
}
3131
}
3232

33-
LFORTRAN_API float _lfortran_random_float()
33+
LFORTRAN_API double _lfortran_random()
3434
{
35-
return ((float) rand() / (float) RAND_MAX);
35+
return (rand() / (double) RAND_MAX);
3636
}
3737

3838
LFORTRAN_API int _lfortran_randrange(int lower, int upper)

‎src/runtime/impure/lfortran_intrinsics.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ typedef double _Complex double_complex_t;
3131

3232
LFORTRAN_API double _lfortran_sum(int n, double *v);
3333
LFORTRAN_API void _lfortran_random_number(int n, double *v);
34-
LFORTRAN_API float _lfortran_random_float();
34+
LFORTRAN_API double _lfortran_random();
3535
LFORTRAN_API int _lfortran_randrange(int lower, int upper);
3636
LFORTRAN_API int _lfortran_random_int(int lower, int upper);
3737
LFORTRAN_API void _lfortran_printf(const char* format, ...);

‎src/runtime/random.py‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
11
from ltypes import i32, f64, ccall
22

3+
e: f64 = 2.718281828459045235360287471352662497757
4+
eps: f64 = 1e-16
5+
6+
#: TODO: Call `log` from C directly until we fix the multiple import issue
7+
def _log(x: f64) -> f64:
8+
return _lfortran_dlog(x)
9+
10+
@ccall
11+
def _lfortran_dlog(x: f64) -> f64:
12+
pass
13+
14+
def _exp(x: f64) -> f64:
15+
return e**x
16+
17+
def _sqrt(x: f64) -> f64:
18+
return x**(1/2)
19+
20+
def _abs(x: f64) -> f64:
21+
if x < 0.0:
22+
return -x
23+
return x
324

425
def random() -> f64:
526
"""
627
Returns a random floating point number in the range [0.0, 1.0)
728
"""
8-
return _lfortran_random_float()
29+
return _lfortran_random()
930

1031
@ccall
11-
def _lfortran_random_float() -> f64:
32+
def _lfortran_random() -> f64:
1233
pass
1334

1435
def randrange(lower: i32, upper: i32) -> i32:
@@ -31,10 +52,32 @@ def randint(lower: i32, upper: i32) -> i32:
3152
def _lfortran_random_int(lower: i32, upper: i32) -> i32:
3253
pass
3354

55+
def uniform(a: f64, b: f64) -> f64:
56+
"""
57+
Get a random number in the range [a, b) or [a, b] depending on rounding.
58+
"""
59+
return a + (b - a) * random()
60+
3461
def paretovariate(alpha: f64) -> f64:
3562
"""
3663
Return a random number from a Pareto distribution with parameter `alpha`.
3764
"""
3865
u: f64
3966
u = 1.0 - random()
4067
return u ** (-1.0 / alpha)
68+
69+
def expovariate(l: f64) -> f64:
70+
"""
71+
Return a random number from an exponential distribution with parameter
72+
`l` (lambda).
73+
"""
74+
assert _abs(l) > eps
75+
return -_log(1.0 - random()) / l
76+
77+
def weibullvariate(alpha: f64, beta: f64) -> f64:
78+
"""
79+
Return a random number from a Weibull distribution with parameters `alpha`
80+
and `beta`.
81+
"""
82+
assert _abs(beta) > eps
83+
return alpha * (-_log(1.0 - random())) ** (1.0 / beta)

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL