Skip to content

Commit e3b7ec8

Browse files
committed
Implement random.paretovariate()
1 parent 2ee64d0 commit e3b7ec8

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

integration_tests/test_random.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,13 @@ def test_randint():
2121
print(ri2)
2222
assert ri2 >= -50 and ri2 <= 76
2323

24+
def test_paretovariate():
25+
r: f64
26+
r = random.paretovariate(2.0)
27+
print(r)
28+
r = random.paretovariate(-5.6)
29+
print(r)
30+
2431
test_random()
2532
test_randint()
33+
test_paretovariate()

src/runtime/random.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from ltypes import i32, f64, ccall
22

3+
34
def random() -> f64:
45
"""
56
Returns a random floating point number in the range [0.0, 1.0)
@@ -19,3 +20,11 @@ def randint(lower: i32, upper: i32) -> i32:
1920
@ccall
2021
def _lfortran_random_int(lower: i32, upper: i32) -> i32:
2122
pass
23+
24+
def paretovariate(alpha: f64) -> f64:
25+
"""
26+
Return a random number from a Pareto distribution with parameter `alpha`.
27+
"""
28+
u: f64
29+
u = 1.0 - random()
30+
return u ** (-1.0 / alpha)

0 commit comments

Comments
 (0)