forked from Rust-related/RustPython
Merge pull request #1540 from palaviv/random-new-arg-style
Convert random to new arg style
This commit is contained in:
@@ -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<f64> {
|
||||
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::<f64>();
|
||||
let py_value = vm.ctx.new_float(value);
|
||||
Ok(py_value)
|
||||
fn random_random(_vm: &VirtualMachine) -> f64 {
|
||||
rand::random()
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user