Skip to content

Commit

Permalink
Implement random.expovariate
Browse files Browse the repository at this point in the history
  • Loading branch information
namannimmo10 committed Mar 23, 2022
1 parent e0fa120 commit e2cde09
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions integration_tests/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,19 @@ def test_paretovariate():
r = random.paretovariate(-5.6)
print(r)

def test_expovariate():
r: f64
r = random.expovariate(2.0)
print(r)
r = random.expovariate(-5.6)
print(r)

def check():
test_random()
test_randrange()
test_randint()
test_uniform()
test_paretovariate()
test_expovariate()

check()
22 changes: 22 additions & 0 deletions src/runtime/random.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
from ltypes import i32, f64, ccall

e: f64 = 2.718281828459045235360287471352662497757

#: TODO: Call `log` from C directly until we fix the multiple import issue
def _log(x: f64) -> f64:
return _lfortran_dlog(x)

@ccall
def _lfortran_dlog(x: f64) -> f64:
pass

def _exp(x: f64) -> f64:
return e**x

def _sqrt(x: f64) -> f64:
return x**(1/2)

def random() -> f64:
"""
Expand Down Expand Up @@ -44,3 +59,10 @@ def paretovariate(alpha: f64) -> f64:
u: f64
u = 1.0 - random()
return u ** (-1.0 / alpha)

def expovariate(l: f64) -> f64:
"""
Return a random number from an exponential distribution with parameter
`l` (lambda).
"""
return -_log(1.0 - random()) / l

0 comments on commit e2cde09

Please sign in to comment.