[][src]Trait turtle::rand::RandomRange

pub trait RandomRange<Bound = Self> {
    fn random_range(low: Bound, high: Bound) -> Self;
}

This trait represents any type that can have random values generated for it within a certain range.

Tip: There is a list later on this page that shows many of the types that implement this trait.

It will usually only make sense to implement this trait for types that represent a single value (e.g. Speed, Color, f64, u32, etc.). For example, if you had the type:

#[derive(Debug, Clone)]
struct Product {
    price: f64,
    quantity: u32,
}

What would random_range(1.5, 5.2) mean for this type? It's hard to say because while you could generate a random value for price, it doesn't make sense to generate a quantity given that range.

A notable exception to this is the Point type. You can interpret a call to random_range(Point {x: 1.0, y: 2.0}, Point {x: 5.0, y: 8.0}) as wanting to generate a random Point in the rectangle formed by the two points provided as arguments.

Example

This example demonstrates how to implement this trait for a type with a limited range of valid values.

use turtle::rand::{Random, RandomRange};

// Some constants to represent the valid range of difficulty levels
const MIN_DIFFICULTY: u32 = 1;
const MAX_DIFFICULTY: u32 = 10;

/// Represents the difficulty level of the game
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct Difficulty(u32);

impl Random for Difficulty {
    /// Generates a random difficulty within the valid range of difficulty levels
    fn random() -> Self {
        Difficulty(RandomRange::random_range(MIN_DIFFICULTY, MAX_DIFFICULTY))
    }
}

// Choosing `u32` for the `Bound` type because that is more convenient to type than if
// we had chosen `Difficulty`.
//
// Example: `random_range(1, 2)` vs. `random_range(Difficulty(1), Difficulty(2))`
impl RandomRange<u32> for Difficulty {
    /// Generates a random difficulty level within the given range.
    ///
    /// # Panics
    ///
    /// Panics if either bound could result in a value outside the valid range
    /// of difficulties or if `low > high`.
    fn random_range(low: u32, high: u32) -> Self {
        if low < MIN_DIFFICULTY || high > MAX_DIFFICULTY {
            panic!("The boundaries must be within the valid range of difficulties");
        }

        RandomRange::random_range(low, high)
    }
}

fn main() {
    use turtle::rand::{random, random_range};

    // We can now generate random values for Difficulty!
    let difficulty: Difficulty = random();
    let difficulty: Difficulty = random_range(5, 10);
}

Required methods

fn random_range(low: Bound, high: Bound) -> Self

Generate a single random value in the given range. The value x that is returned will be such that low ≤ x ≤ high.

The Bound type is used to represent the boundaries of the range of values to generate. For most types this will just be Self.

Panics

Panics if low > high.

Loading content...

Implementations on Foreign Types

impl RandomRange<f32> for f32[src]

impl RandomRange<f64> for f64[src]

impl RandomRange<u8> for u8[src]

impl RandomRange<u16> for u16[src]

impl RandomRange<u32> for u32[src]

impl RandomRange<u64> for u64[src]

impl RandomRange<u128> for u128[src]

impl RandomRange<usize> for usize[src]

impl RandomRange<i8> for i8[src]

impl RandomRange<i16> for i16[src]

impl RandomRange<i32> for i32[src]

impl RandomRange<i64> for i64[src]

impl RandomRange<i128> for i128[src]

impl RandomRange<isize> for isize[src]

Loading content...

Implementors

impl<B: Into<Color>> RandomRange<B> for Color[src]

impl<B: Into<Point>> RandomRange<B> for Point[src]

impl<B: Into<Speed>> RandomRange<B> for Speed[src]

fn random_range(low: B, high: B) -> Self[src]

Generates a random difficulty level within the given range, not including instant.

Panics

Panics if either bound could result in a value outside the valid range of speed levels or if low > high. Also panics if either bound is Speed::instant().

Loading content...