pub struct Random { /* private fields */ }Expand description
A uniformly random option, from PHP’s Mersenne Twister.
Random::seeded replays exactly what PHP picks after mt_srand($seed),
so a choice can be reproduced in tests and compared with PHP.
PHP: Utopia\Balancer\Algorithm\Random (array_rand() on the default
engine).
use utopia_balancer::{Algorithm, Random};
let options = ["fra-1", "fra-2", "lon-1"];
let refs: Vec<&&str> = options.iter().collect();
let mut random = Random::seeded(7);
let picks: Vec<_> = (0..3).map(|_| random.pick(&refs).unwrap().unwrap()).collect();
assert_eq!(picks, [0, 1, 1]);use Utopia\Balancer\Algorithm\Random;
use Utopia\Balancer\Option;
$options = [new Option(['dc' => 'fra-1']), new Option(['dc' => 'fra-2']), new Option(['dc' => 'lon-1'])];
mt_srand(7);
$random = new Random();
foreach ([1, 2, 3] as $_) {
echo $random->run($options)->getState('dc'), "\n"; // fra-1, fra-2, fra-2
}Implementations§
Trait Implementations§
Auto Trait Implementations§
impl Forget for Random
impl Freeze for Random
impl Move for Random
impl RefUnwindSafe for Random
impl Send for Random
impl Sync for Random
impl Unpin for Random
impl UnsafeUnpin for Random
impl UnwindSafe for Random
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more