[][src]Function turtle::rand::random

pub fn random<T: Random>() -> T

Generates a single random value of the type T.

Specifying the type to generate

Since T can be any of a number of different types, you may need to provide a "type parameter" explicitly so that Rust knows what to generate. You can do this either by annotating the type of the variable you are assigning to or by using "turbofish" syntax to specify the type on the random() function itself.

use turtle::{Turtle, Speed, rand::random};
let mut turtle = Turtle::new();

// 1. Separate out into a variable, then annotate the desired type
let speed: Speed = random();
turtle.set_speed(speed);

// 2. Turbofish syntax ::<T>
turtle.set_speed(random::<Speed>());

Example

Setting the pen color to a randomly generated color:

use turtle::{Turtle, Color, rand::random};

let mut turtle = Turtle::new();
let color: Color = random();
// Calling `opaque()` because even the `alpha` value of the color is randomized.
turtle.set_pen_color(color.opaque());