Skip to content

Initial implementation of the random module #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integration_tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"modules_01.py",
#"modules_02.py",
"test_math.py",
"test_random.py",
"test_builtin.py",
"test_builtin_abs.py",
"test_builtin_bool.py",
Expand Down
43 changes: 43 additions & 0 deletions integration_tests/test_random.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from ltypes import i32, f64
import random


def test_random():
r: f64
r = random.random()
print(r)
assert r >= 0.0 and r < 1.0
r = random.random()
print(r)
assert r >= 0.0 and r < 1.0

def test_randrange():
r: i32
r = random.randrange(0, 10) # [0, 10)
print(r)
assert r >= 0 and r < 10
r = random.randrange(-50, 76) # [-50, 76)
print(r)
assert r >= -50 and r < 76

def test_randint():
ri1: i32
ri2: i32
ri1 = random.randint(0, 10) # [0, 10]
print(ri1)
assert ri1 >= 0 and ri1 <= 10
ri2 = random.randint(-50, 76) # [-50, 76]
print(ri2)
assert ri2 >= -50 and ri2 <= 76

def test_paretovariate():
r: f64
r = random.paretovariate(2.0)
print(r)
r = random.paretovariate(-5.6)
print(r)

test_random()
test_randrange()
test_randint()
test_paretovariate()
17 changes: 17 additions & 0 deletions src/runtime/impure/lfortran_intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ LFORTRAN_API void _lfortran_random_number(int n, double *v)
}
}

LFORTRAN_API float _lfortran_random_float()
{
return ((float) rand() / (float) RAND_MAX);
}

LFORTRAN_API int _lfortran_randrange(int lower, int upper)
{
int rr = lower + (rand() % (upper - lower));
return rr;
}

LFORTRAN_API int _lfortran_random_int(int lower, int upper)
{
int randint = lower + (rand() % (upper - lower + 1));
return randint;
}

LFORTRAN_API void _lfortran_printf(const char* format, ...)
{
va_list args;
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/impure/lfortran_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ typedef double _Complex double_complex_t;

LFORTRAN_API double _lfortran_sum(int n, double *v);
LFORTRAN_API void _lfortran_random_number(int n, double *v);
LFORTRAN_API float _lfortran_random_float();
LFORTRAN_API int _lfortran_randrange(int lower, int upper);
LFORTRAN_API int _lfortran_random_int(int lower, int upper);
LFORTRAN_API void _lfortran_printf(const char* format, ...);
LFORTRAN_API void _lfortran_complex_add(struct _lfortran_complex* a,
struct _lfortran_complex* b, struct _lfortran_complex *result);
Expand Down
40 changes: 40 additions & 0 deletions src/runtime/random.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from ltypes import i32, f64, ccall


def random() -> f64:
"""
Returns a random floating point number in the range [0.0, 1.0)
"""
return _lfortran_random_float()

@ccall
def _lfortran_random_float() -> f64:
pass

def randrange(lower: i32, upper: i32) -> i32:
"""
Return a random integer N such that `lower <= N < upper`.
"""
return _lfortran_randrange(lower, upper)

@ccall
def _lfortran_randrange(lower: i32, upper: i32) -> i32:
pass

def randint(lower: i32, upper: i32) -> i32:
"""
Return a random integer N such that `lower <= N <= upper`.
"""
return _lfortran_random_int(lower, upper)

@ccall
def _lfortran_random_int(lower: i32, upper: i32) -> i32:
pass

def paretovariate(alpha: f64) -> f64:
"""
Return a random number from a Pareto distribution with parameter `alpha`.
"""
u: f64
u = 1.0 - random()
return u ** (-1.0 / alpha)