1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#[allow(unused_imports)]
use rhai::plugin::*;
#[export_module]
pub mod rand_functions {
use rand::prelude::*;
use rand::distributions::{Alphanumeric, DistString};
use rhai::{EvalAltResult, Position, INT};
use std::ops::{Range, RangeInclusive};
#[cfg(feature = "float")]
use rhai::FLOAT;
#[cfg(feature = "decimal")]
use rust_decimal::Decimal;
/// Generate a random boolean value.
///
/// ### Example
///
/// ```rhai
/// let decision = rand_bool();
///
/// if decision {
/// print("You hit the Jackpot!")
/// }
/// ```
pub fn rand_bool() -> bool {
rand::random()
}
/// Generate a random alphanumeric string of a specified length.
///
/// The string will contain random uppercase letters (A-Z), lowercase letters (a-z),
/// and digits (0-9).
///
/// ### Example
///
/// ```rhai
/// let random_file_name= rand_alpha_numeric(12);
///
/// print(`Random file name: ${random_file_name}`);
/// ```
#[rhai_fn(name = "rand_alpha_numeric", return_raw)]
pub fn rand_alpha_numeric_of_length(x: INT) -> Result<String, Box<EvalAltResult>> {
if x <= 0 {
Err(EvalAltResult::ErrorArithmetic(
format!("String length must be positive: {}", x),
Position::NONE,
)
.into())
} else {
Ok(Alphanumeric.sample_string(&mut rand::thread_rng(), x as usize))
}
}
/// Generate a random boolean value with a probability of being `true`.
/// Requires the `float` feature.
///
/// `probability` must be between `0.0` and `1.0` (inclusive).
///
/// ### Example
///
/// ```rhai
/// let decision = rand_bool(0.01); // 1% probability
///
/// if decision {
/// print("You hit the Jackpot!")
/// }
/// ```
#[cfg(feature = "float")]
#[rhai_fn(name = "rand_bool", return_raw)]
pub fn rand_bool_with_probability(probability: FLOAT) -> Result<bool, Box<EvalAltResult>> {
if probability < 0.0 || probability > 1.0 {
Err(EvalAltResult::ErrorArithmetic(
format!(
"Invalid probability (must be between 0.0 and 1.0): {}",
probability
),
Position::NONE,
)
.into())
} else {
Ok(rand::thread_rng().gen_bool(probability as f64))
}
}
/// Generate a random integer number.
///
/// ### Example
///
/// ```rhai
/// let number = rand();
///
/// print(`I'll give you a random number: ${number}`);
/// ```
pub fn rand() -> INT {
rand::random()
}
/// Generate a random integer number within an exclusive range.
///
/// ### Example
///
/// ```rhai
/// let number = rand(18..39);
///
/// print(`I'll give you a random number between 18 and 38: ${number}`);
/// ```
#[rhai_fn(name = "rand", return_raw)]
pub fn rand_exclusive_range(range: Range<INT>) -> Result<INT, Box<EvalAltResult>> {
if range.is_empty() {
Err(EvalAltResult::ErrorArithmetic(
format!("Range is empty: {:?}", range),
Position::NONE,
)
.into())
} else {
Ok(rand::thread_rng().gen_range(range))
}
}
/// Generate a random integer number within an inclusive range.
///
/// ### Example
///
/// ```rhai
/// let number = rand(18..=38);
///
/// print(`I'll give you a random number between 18 and 38: ${number}`);
/// ```
#[rhai_fn(name = "rand", return_raw)]
pub fn rand_inclusive_range(range: RangeInclusive<INT>) -> Result<INT, Box<EvalAltResult>> {
if range.is_empty() {
Err(EvalAltResult::ErrorArithmetic(
format!("Range is empty: {:?}", range),
Position::NONE,
)
.into())
} else {
Ok(rand::thread_rng().gen_range(range))
}
}
/// Generate a random integer number within an inclusive range.
///
/// ### Example
///
/// ```rhai
/// let number = rand(18, 38);
///
/// print(`I'll give you a random number between 18 and 38: ${number}`);
/// ```
#[rhai_fn(name = "rand", return_raw)]
pub fn rand_from_to_inclusive(start: INT, end: INT) -> Result<INT, Box<EvalAltResult>> {
if start >= end {
Err(EvalAltResult::ErrorArithmetic(
format!("Range is empty: {}..{}", start, end),
Position::NONE,
)
.into())
} else {
Ok(rand::thread_rng().gen_range(start..=end))
}
}
/// Generate a random floating-point number between `0.0` and `1.0` (exclusive).
/// Requires the `float` feature.
///
/// `1.0` is _excluded_ from the possibilities.
///
/// ### Example
///
/// ```rhai
/// let number = rand_float();
///
/// print(`I'll give you a random number between 0 and 1: ${number}`);
/// ```
#[cfg(feature = "float")]
pub fn rand_float() -> FLOAT {
rand::random()
}
/// Generate a random floating-point number within an exclusive range.
/// Requires the `float` feature.
///
/// ### Example
///
/// ```rhai
/// let number = rand_float(123.456, 789.678);
///
/// print(`I'll give you a random number between 123.456 and 789.678: ${number}`);
/// ```
#[cfg(feature = "float")]
#[rhai_fn(name = "rand_float", return_raw)]
pub fn rand_float_range(start: FLOAT, end: FLOAT) -> Result<FLOAT, Box<EvalAltResult>> {
if start >= end {
Err(EvalAltResult::ErrorArithmetic(
format!("Range is empty: {}..{}", start, end),
Position::NONE,
)
.into())
} else {
Ok(rand::thread_rng().gen_range(start..=end))
}
}
/// Generate a random [decimal](https://crates.io/crates/rust_decimal) number.
/// Requires the `decimal` feature.
///
/// ### Example
///
/// ```rhai
/// let number = rand_decimal();
///
/// print(`I'll give you a random decimal number: ${number}`);
/// ```
#[cfg(feature = "decimal")]
pub fn rand_decimal() -> Decimal {
rand::random()
}
/// Generate a random [decimal](https://crates.io/crates/rust_decimal) number within a range.
/// Requires the `decimal` feature.
///
/// ### Example
///
/// ```rhai
/// let number = rand(18.to_decimal(), 38.to_decimal());
///
/// print(`I'll give you a random number between 18 and 38: ${number}`);
/// ```
#[cfg(feature = "decimal")]
#[rhai_fn(name = "rand_decimal", return_raw)]
pub fn rand_decimal_range(start: Decimal, end: Decimal) -> Result<Decimal, Box<EvalAltResult>> {
if start >= end {
Err(EvalAltResult::ErrorArithmetic(
format!("Range is empty: {}..{}", start, end),
Position::NONE,
)
.into())
} else {
Ok(rand::thread_rng().gen_range(start..=end))
}
}
}