Generate and Extract Random Data with RandoPHP

Packages

October 22nd, 2020

sequence-featured.png

RandoPHP is a package by Roberto Butti that implements random generators (Integer, Char, Byte, etc.) and takes random samples from arrays. This package can be broken down into two distinct types of operations: generating things and drawing from existing arrays.

RandoPHP provides a fluent interface to generate integers, dates, floats, etc. to help you generate data:

1// Generate char
2Randomize::char()->numeric()->generate();
3Randomize::char()->alpha()->generate();
4Randomize::char()->alphanumeric()->generate();
5 
6// Generate a boolean (flip a coin)
7Randomize::boolean()->generate();
8 
9// Generate floats and integers
10$randomFloat = Randomize::float()->generate();
11$randomFloat = Randomize::float()->min(0)->max(90)->generate();
12$randomNumber = Randomize::integer()->min(1)->max(6)->generate();
13$randomNumber = Randomize::integer()->range(1,6)->generate();
14 
15// Generate sequences (i.e. roll the dice 15 times)
16$randomRolls = Randomize::sequence()
17 ->min(1)
18 ->max(6)
19 ->count(15)
20 ->generate();
21 
22// Generate a random sequence of 10 alpha-numeric chars
23Randomize::sequence()
24 ->chars()
25 ->alphanumeric()
26 ->count(10)
27 ->generate();

Secondly, you can draw random stuff from existing arrays using the Draw class:

1<br></br>$array=["React.js", "Vue.js", "Svelte.js", "Angular.js" , "Alpine.js", "Vanilla js"];
2 
3// Draw one
4$randomJs = Draw::sample($array)->extract();
5 
6// Draw three
7$randomJs = Draw::sample($array)->count(3)->extract();
8 
9// Draw three, duplicates allowed
10$randomJs = Draw::sample($array)
11 ->count(3)
12 ->allowDuplicates()
13 ->extract();

The examples in this article were taken from the project’s readme, which has full details on usage. You can view this package’s source code on GitHub at Hi-Folks/rando-php.


This package was submitted to our Laravel News Links section. Links is a place the community can post packages and tutorials around the Laravel ecosystem. Follow along on Twitter @LaravelLinks.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.