MCPcopy Create free account
hub / github.com/aiscriptdev/aiscript / random_int

Function random_int

aiscript-vm/src/stdlib/random.rs:77–98  ·  view source on GitHub ↗
(_state: &mut State<'gc>, args: Vec<Value<'gc>>)

Source from the content-addressed store, hash-verified

75}
76
77fn random_int<'gc>(_state: &mut State<'gc>, args: Vec<Value<'gc>>) -> Result<Value<'gc>, VmError> {
78 if args.len() != 2 {
79 return Err(VmError::RuntimeError(
80 "randint() takes exactly 2 arguments: min and max".into(),
81 ));
82 }
83
84 let min = float_arg!(&args, 0, "randint")? as i64;
85 let max = float_arg!(&args, 1, "randint")? as i64;
86
87 if min > max {
88 return Err(VmError::RuntimeError(
89 "randint(): max must be greater than min".into(),
90 ));
91 }
92
93 RNG.with(|rng| {
94 let dist = Uniform::new_inclusive(min, max).unwrap();
95 let val = dist.sample(&mut *rng.borrow_mut());
96 Ok(Value::Number(val as f64))
97 })
98}
99
100fn random_uniform<'gc>(
101 _state: &mut State<'gc>,

Callers

nothing calls this directly

Calls 3

RuntimeErrorClass · 0.85
lenMethod · 0.80
borrow_mutMethod · 0.45

Tested by

no test coverage detected