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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! # `rhai-rand` - Rhai Functions for Random Number Generation
//!
//! `rhai-rand` is a [Rhai] package to provide random number generation using the [`rand`] crate.
//!
//!
//! ## Usage
//!
//!
//! ### `Cargo.toml`
//!
//! ```toml
//! [dependencies]
//! rhai-rand = "0.1"
//! ```
//!
//! ### [Rhai] script
//!
//! ```js
//! // Create random boolean
//! let decision = rand_bool();
//!
//! if decision {
//!     // Create random number
//!     let random_value = rand();
//!     print(`Random number = ${random_value}`);
//! } else {
//!     print("Fixed number = 42");
//! }
//!
//! // Create array
//! let a = [1, 2, 3, 4, 5];
//!
//! // Shuffle it!
//! a.shuffle();
//!
//! // Now the array is shuffled randomly!
//! print(a);
//!
//! // Sample a random value from the array
//! print(a.sample());
//!
//! // Or sample multiple values
//! print(a.sample(3));
//! ```
//!
//! ### Rust source
//!
//! ```rust
//! # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
//! use rhai::Engine;
//! use rhai::packages::Package;
//!
//! use rhai_rand::RandomPackage;
//!
//! // Create Rhai scripting engine
//! let mut engine = Engine::new();
//!
//! // Create random number package and add the package into the engine
//! engine.register_global_module(RandomPackage::new().as_shared_module());
//!
//! // Print 10 random numbers, each of which between 0-100!
//! for _ in 0..10 {
//!     let value = engine.eval::<i64>("rand(0..=100)")?;
//!
//!     println!("Random number = {}", value);
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Features
//!
//! |  Feature   | Default  | Description                                                                        |
//! | :--------: | :------: | ---------------------------------------------------------------------------------- |
//! | `metadata` | disabled | includes functions metadata: parameter names/types, return type, doc-comments      |
//! | `decimal`  | disabled | provides random [decimal](https://crates.io/crates/rust_decimal) number generation |
//! |  `float`   | enabled  | provides random floating-point number generation                                   |
//! |  `array`   | enabled  | provides methods for [Rhai] arrays                                                 |
//!
//!
//! ## API
//!
//! The following functions are defined in this package:
//!
//! |          Function          | Return value |  Feature  | Description                                                            |
//! | :------------------------: | :----------: | :-------: | ---------------------------------------------------------------------- |
//! |          `rand()`          |    `INT`     |           | generates a random integer number                                      |
//! |     `rand(start..end)`     |    `INT`     |           | generates a random integer number within an exclusive range            |
//! |    `rand(start..=end)`     |    `INT`     |           | generates a random integer number within an inclusive range            |
//! |     `rand(start, end)`     |    `INT`     |           | generates a random integer number within a range                       |
//! |       `rand_float()`       |   `FLOAT`    |  `float`  | generates a random floating-point number between `0.0` and `1.0`       |
//! |  `rand_float(start, end)`  |   `FLOAT`    |  `float`  | generates a random floating-point number an exclusive range            |
//! |      `rand_decimal()`      | [`Decimal`]  | `decimal` | generates a random [decimal][`Decimal`] number                         |
//! | `rand_decimal(start, end)` | [`Decimal`]  | `decimal` | generates a random [decimal][`Decimal`] number within a range          |
//! |       `rand_bool()`        |    `bool`    |           | generates a random boolean                                             |
//! |       `rand_bool(p)`       |    `bool`    |           | generates a random boolean with the probability `p` of being `true`    |
//! |     `Array.shuffle()`      |              |  `array`  | shuffles the items in the [Rhai] array                                 |
//! |      `Array.sample()`      |  `Dynamic`   |  `array`  | copies a random element from the [Rhai] array                          |
//! |     `Array.sample(n)`      |   `Array`    |  `array`  | copies a non-repeating random sample of elements from the [Rhai] array |
//!
//!
//! [Rhai]: https://rhai.rs
//! [`rand`]: https://crates.io/crates/rand
//! [`Decimal`]: https://crates.io/crates/rust_decimal

use rand::prelude::*;
use rhai::def_package;
use rhai::plugin::*;
use rhai::{EvalAltResult, Position, INT};
use std::ops::{Range, RangeInclusive};

#[cfg(feature = "float")]
use rhai::FLOAT;

#[cfg(feature = "array")]
use rhai::Array;

def_package! {
    /// Package for random number generation, sampling and shuffling.
    pub RandomPackage(lib) {
        combine_with_exported_module!(lib, "rand", rand_functions);

        #[cfg(feature = "array")]
        combine_with_exported_module!(lib, "array", array_functions);
    }
}

#[export_module]
mod rand_functions {
    /// 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 boolean value with a probability of being `true`.
    ///
    /// `probability` must be between `0.0` and `1.0` (inclusive).
    ///
    /// # Example
    ///
    /// ```rhai
    /// let decision = rand(0.01);  // 1% probability
    ///
    /// if decision {
    ///     print("You hit the Jackpot!")
    /// }
    /// ```
    #[cfg(feature = "float")]
    #[rhai_fn(name = "rand", 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).
    ///
    /// `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.
    ///
    /// # 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.
    ///
    /// # Example
    ///
    /// ```rhai
    /// let number = rand_decimal();
    ///
    /// print(`I'll give you a random decimal number: ${number}`);
    /// ```
    #[cfg(feature = "decimal")]
    pub fn rand_decimal() -> rust_decimal::Decimal {
        rand::random()
    }
    /// Generate a random [decimal](https://crates.io/crates/rust_decimal) number within a range.
    ///
    /// # 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: rust_decimal::Decimal,
        end: rust_decimal::Decimal,
    ) -> Result<rust_decimal::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))
        }
    }
}

#[cfg(feature = "array")]
#[export_module]
mod array_functions {
    /// Copy a random element from the array and return it.
    ///
    /// # Example
    ///
    /// ```rhai
    /// let x = [1, 2, 3, 4, 5];
    ///
    /// let number = x.sample();
    ///
    /// print(`I'll give you a random number between 1 and 5: ${number}`);
    /// ```
    #[rhai_fn(global)]
    pub fn sample(array: &mut Array) -> rhai::Dynamic {
        if !array.is_empty() {
            let mut rng = rand::thread_rng();
            if let Some(res) = array.choose(&mut rng) {
                return res.clone();
            }
        }
        Dynamic::UNIT
    }

    /// Copy a non-repeating random sample of elements from the array and return it.
    ///
    /// Elements in the return array are likely not in the same order as in the original array.
    ///
    /// * If `amount` ≤ 0, the empty array is returned.
    /// * If `amount` ≥ length of array, the entire array is returned, but shuffled.
    ///
    /// # Example
    ///
    /// ```rhai
    /// let x = [1, 2, 3, 4, 5];
    ///
    /// let samples = x.sample(3);
    ///
    /// print(`I'll give you 3 random numbers between 1 and 5: ${samples}`);
    /// ```
    #[rhai_fn(global, name = "sample")]
    pub fn sample_with_amount(array: &mut Array, amount: rhai::INT) -> Array {
        if array.is_empty() || amount <= 0 {
            return Array::new();
        }

        let mut rng = rand::thread_rng();
        let amount = amount as usize;

        if amount >= array.len() {
            let mut res = array.clone();
            res.shuffle(&mut rng);
            res
        } else {
            let mut res: Array = array.choose_multiple(&mut rng, amount).cloned().collect();
            // Although the elements are selected randomly, the order of elements in
            // the buffer is neither stable nor fully random. So we must shuffle the
            // result to achieve random ordering.
            res.shuffle(&mut rng);
            res
        }
    }

    /// Shuffle the elements in the array.
    ///
    /// # Example
    ///
    /// ```rhai
    /// let x = [1, 2, 3, 4, 5];
    ///
    /// x.shuffle();    // shuffle the elements inside the array
    /// ```
    #[rhai_fn(global)]
    pub fn shuffle(array: &mut Array) {
        let mut rng = rand::thread_rng();
        array.shuffle(&mut rng);
    }
}