diff --git a/vm/src/stdlib/random.rs b/vm/src/stdlib/random.rs index 38f3f78ba5..398ce749b7 100644 --- a/vm/src/stdlib/random.rs +++ b/vm/src/stdlib/random.rs @@ -3,8 +3,6 @@ use rand::distributions::Distribution; use rand_distr::Normal; -use crate::function::PyFuncArgs; -use crate::obj::objfloat; use crate::pyobject::{PyObjectRef, PyResult}; use crate::vm::VirtualMachine; @@ -12,29 +10,14 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { let ctx = &vm.ctx; py_module!(vm, "random", { - "guass" => ctx.new_rustfunc(random_gauss), + "gauss" => ctx.new_rustfunc(random_normalvariate), // TODO: is this the same? "normalvariate" => ctx.new_rustfunc(random_normalvariate), "random" => ctx.new_rustfunc(random_random), // "weibull", ctx.new_rustfunc(random_weibullvariate), }) } -fn random_gauss(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - // TODO: is this the same? - random_normalvariate(vm, args) -} - -fn random_normalvariate(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - arg_check!( - vm, - args, - required = [ - (mu, Some(vm.ctx.float_type())), - (sigma, Some(vm.ctx.float_type())) - ] - ); - let mu = objfloat::get_value(mu); - let sigma = objfloat::get_value(sigma); +fn random_normalvariate(mu: f64, sigma: f64, vm: &VirtualMachine) -> PyResult { let normal = Normal::new(mu, sigma).map_err(|rand_err| { vm.new_exception( vm.ctx.exceptions.arithmetic_error.clone(), @@ -42,15 +25,11 @@ fn random_normalvariate(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { ) })?; let value = normal.sample(&mut rand::thread_rng()); - let py_value = vm.ctx.new_float(value); - Ok(py_value) + Ok(value) } -fn random_random(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - arg_check!(vm, args); - let value = rand::random::(); - let py_value = vm.ctx.new_float(value); - Ok(py_value) +fn random_random(_vm: &VirtualMachine) -> f64 { + rand::random() } /*