← Lararand 7 / 9

API

use EduLazaro\Lararand\Facades\Rand;

Rand::bytes(32);                    // raw bytes
Rand::below(78);                    // 0 to 77
Rand::int(1, 6);                    // 1 to 6, both included
Rand::ints(5, 1, 6);                // five dice, repeats allowed
Rand::distinct(6, 49);              // six of 49, no repeats, random order
Rand::shuffle($items);              // a permutation; keys are dropped
Rand::pick($items, 3);              // three items, no item twice
Rand::one($items);                  // one item
Rand::float();                      // 0.0 to 1.0, exclusive at the top
Rand::string(12, 'ABCDEF0123456789');

Every one of these is on EduLazaro\Lararand\Randomness, which you can inject instead of using the facade wherever you would rather say what you depend on.

Integers

below(int $bound) gives 0 to $bound - 1. It is the primitive: everything else that needs a number in a range goes through it, and it is where rejection sampling happens.

int(int $min, int $max) includes both ends, which is what people expect from a die.

ints(int $count, int $min, int $max) is $count independent draws. Repeats are not only allowed, they are the point: five dice can come up all sixes.

Without replacement

distinct(int $count, int $bound) gives $count different values from 0 to $bound - 1, in random order. Six of 49 for a lottery, seven of 78 for a spread of cards.

pick(array $items, int $count = 1) is the same over your own array, and never returns an item twice. one(array $items) is the single-item version and returns the item, not an array of one.

shuffle(array $items) returns a permutation. Keys are dropped, because a shuffled associative array is almost always a bug rather than a request.

The rest

bytes(int $count) is the raw source, if you are deriving something the package does not offer.

float() is 0.0 to 1.0, exclusive at the top. Exclusive because half-open is the interval every other range in the language uses, and a 1.0 that turns up once in a few billion draws is a bug you will find in production rather than in a test.

string(int $length, string $alphabet) draws from the alphabet you give it. The default is alphanumeric. It is not a password generator and does not pretend to be one: it draws uniformly from what you pass, and excluding lookalike characters is your call.