Skip to content

Commit 2bfb08a

Browse files
authored
Merge pull request #1540 from palaviv/random-new-arg-style
Convert random to new arg style
2 parents 7980b78 + cd96d2b commit 2bfb08a

1 file changed

Lines changed: 5 additions & 26 deletions

File tree

vm/src/stdlib/random.rs

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,33 @@
33
use rand::distributions::Distribution;
44
use rand_distr::Normal;
55

6-
use crate::function::PyFuncArgs;
7-
use crate::obj::objfloat;
86
use crate::pyobject::{PyObjectRef, PyResult};
97
use crate::vm::VirtualMachine;
108

119
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
1210
let ctx = &vm.ctx;
1311

1412
py_module!(vm, "random", {
15-
"guass" => ctx.new_rustfunc(random_gauss),
13+
"gauss" => ctx.new_rustfunc(random_normalvariate), // TODO: is this the same?
1614
"normalvariate" => ctx.new_rustfunc(random_normalvariate),
1715
"random" => ctx.new_rustfunc(random_random),
1816
// "weibull", ctx.new_rustfunc(random_weibullvariate),
1917
})
2018
}
2119

22-
fn random_gauss(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
23-
// TODO: is this the same?
24-
random_normalvariate(vm, args)
25-
}
26-
27-
fn random_normalvariate(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
28-
arg_check!(
29-
vm,
30-
args,
31-
required = [
32-
(mu, Some(vm.ctx.float_type())),
33-
(sigma, Some(vm.ctx.float_type()))
34-
]
35-
);
36-
let mu = objfloat::get_value(mu);
37-
let sigma = objfloat::get_value(sigma);
20+
fn random_normalvariate(mu: f64, sigma: f64, vm: &VirtualMachine) -> PyResult<f64> {
3821
let normal = Normal::new(mu, sigma).map_err(|rand_err| {
3922
vm.new_exception(
4023
vm.ctx.exceptions.arithmetic_error.clone(),
4124
format!("invalid normal distribution: {:?}", rand_err),
4225
)
4326
})?;
4427
let value = normal.sample(&mut rand::thread_rng());
45-
let py_value = vm.ctx.new_float(value);
46-
Ok(py_value)
28+
Ok(value)
4729
}
4830

49-
fn random_random(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
50-
arg_check!(vm, args);
51-
let value = rand::random::<f64>();
52-
let py_value = vm.ctx.new_float(value);
53-
Ok(py_value)
31+
fn random_random(_vm: &VirtualMachine) -> f64 {
32+
rand::random()
5433
}
5534

5635
/*

0 commit comments

Comments
 (0)