Skip to main content

Random

Struct Random 

Source
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§

Source§

impl Random

Source

pub fn new() -> Self

Picks seeded from the operating system.

PHP: new Random(), on the default engine PHP seeds at startup.

Source

pub fn seeded(seed: i64) -> Self

Picks that PHP makes after mt_srand($seed).

Trait Implementations§

Source§

impl<T: ?Sized> Algorithm<T> for Random

Source§

fn name(&self) -> &str

Random.

Source§

fn pick(&mut self, options: &[&T]) -> Result<Option<usize>, Error>

A random position. Without options this is an Error::Value.

PHP: array_rand() throws a ValueError for an empty array.

Source§

impl Clone for Random

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Random

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Random

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.