Skip to content

Commit 9213c99

Browse files
committed
upgrade direct use of rand
1 parent 45e4d6e commit 9213c99

File tree

3 files changed

+95
-4
lines changed

3 files changed

+95
-4
lines changed

Cargo.lock

Lines changed: 85 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vm/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ num-traits = "0.2.8"
3030
num-integer = "0.1.41"
3131
num-rational = "0.2.2"
3232
num-iter = "0.1.39"
33-
rand = "0.5"
33+
rand = "0.7"
34+
rand_distr = "0.2"
3435
log = "0.3"
3536
rustpython-derive = {path = "../derive", version = "0.1.1"}
3637
rustpython-parser = {path = "../parser", optional = true, version = "0.1.1"}

vm/src/stdlib/random.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Random module.
22
3-
use rand::distributions::{Distribution, Normal};
3+
use rand::distributions::Distribution;
4+
use rand_distr::Normal;
45

56
use crate::function::PyFuncArgs;
67
use crate::obj::objfloat;
@@ -34,7 +35,12 @@ fn random_normalvariate(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
3435
);
3536
let mu = objfloat::get_value(mu);
3637
let sigma = objfloat::get_value(sigma);
37-
let normal = Normal::new(mu, sigma);
38+
let normal = Normal::new(mu, sigma).map_err(|rand_err| {
39+
vm.new_exception(
40+
vm.ctx.exceptions.arithmetic_error.clone(),
41+
format!("invalid normal distribution: {:?}", rand_err),
42+
)
43+
})?;
3844
let value = normal.sample(&mut rand::thread_rng());
3945
let py_value = vm.ctx.new_float(value);
4046
Ok(py_value)

0 commit comments

Comments
 (0)