Skip to content

RandoPhp is a open source library that implements random generators (Integer, Char, Byte, Sequences, Boolean) and take random sample from arrays

License

Notifications You must be signed in to change notification settings

Hi-Folks/rando-php

Repository files navigation

Rando-PHP

Testing RandoPHP GitHub last commit GitHub Release Date Packagist PHP Version Support

RandoPHP

RandoPHP is a PHP open-source package for random stuff. With this package you can:

  • Draw: Extract random items (sample) from an array. This is useful when you want to "draw" some numbers or items;
  • Generate: useful for create random items/sequences:
    • item like integer, byte, boolean, float, lat/long coordinates, char (numeric, alphabetic, alphanumeric);
    • sequences like an array of integers or chars;

With the fluent interface, you can control some things like:

  • minimum and maximum value you want to create;
  • how many items you want to create;
  • for sequences if you want or not duplicates ([1,5,3,1,1], 1 is duplicate or [1,6,5,3,8], no duplicates);
  • And other stuff, see the documentation for more options.

Table of contents

Installation

You can install the package via composer:

composer require hi-folks/rando-php

Usage

To import the right class:

use HiFolks\RandoPhp\Randomize;

Generate Char

Sometimes you want to obtain a random char, for example, a numeric char:

Randomize::char()->numeric()->generate();

Or you might want an alphabetic char:

Randomize::char()->alpha()->generate();

You can even do both!

Randomize::char()->alphanumeric()->generate();

You can generate a lower case char:

Randomize::char()->alpha()->lower()->generate();

You can generate an upper case char:

Randomize::char()->alpha()->upper()->generate();

You can generate a special character. A special character is one of '!"#$%&'()*+,-./:;<=>?@[]^_`{|}~':

Randomize::char()->specialCharacters()->generate();

Generate Boolean

Sometimes you want to obtain a random boolean true or false (flip a coin):

$randomBool = Randomize::boolean()->generate();

Generate a Float

Sometimes you want to obtain a random float (default min-max range of 0.0 - 1.0). For example, you want to generate a random temperature for a day:

$randomFloat = Randomize::float()->generate();

Or you can set the min-max range of 0 - 90, which is equivalent to ->min(0)->max(90)

$randomFloat = Randomize::float()->min(0)->max(90)->generate();

Generate an Integer

Sometimes you want to obtain a random integer (min-max range). For example, you want to roll the dice:

$randomNumber = Randomize::integer()->min(1)->max(6)->generate();

The same thing using range() method, instead of min() and max():

$randomNumber = Randomize::integer()->range(1,6)->generate();

You can use a shortcut helper to generate an integer calling the constructor with min and max parameters:

$randomNumber = Randomize::integer(1,6)->generate();

Generate bytes

Sometimes you want to obtain some random bytes (hexadecimal). For example, you want to generate a random RGB color (a hex triplet in hexadecimal format):

$randomRGB = Randomize::byte()->length(3)->generate();

Generate a Date

Sometimes you want to obtain a random date. For example, you want to generate a random date:

$randomDate = Randomize::datetime()->generate();

By default, a date between the first day of the current year to the last day of the current year. If you want to define the range, for example, a range of 01 Jan 2020 to 10 Jan 2020, you can use min() and max() methods:

$randomDate = Randomize::datetime()->min('2020–01–01')->max('2020–01–10')->generate();

You can even specify your preferred format for the random date generated, by using the format() method:

$randomDate = Randomize::datetime()->format('d-M-Y')->generate();

You can use min() and max() with format() method:

$randomDate = Randomize::datetime()->min('2020–01–01')->max('2020–01–10')->format('d-M-Y')->generate();

With the latest example, you can obtain something like "05-Jan-2020".

Generate sequences

Sometimes, you want to obtain some random sequences. For example, you want to roll the dice 15 times:

$randomRolls = Randomize::sequence()->min(1)->max(6)->count(15)->generate();

Sometimes you want to obtain some random char sequences. For example, char sequences of length 10:

$randomChars = Randomize::sequence()->chars()->count(10)->generate();

Or you might want numeric char sequences.

$randomChars = Randomize::sequence()->chars()->numeric()->count(10)->generate();

Or you might want alphabetical char sequences.

$randomChars = Randomize::sequence()->chars()->alpha()->count(10)->generate();

Yes, even both.

$randomChars = Randomize::sequence()->chars()->alphanumeric()->count(10)->generate();

You want 20 lowercase chars:

Randomize::sequence()->chars()->alphaLowerCase()->count(20)->asString()->generate();

You want 20 upper case chars:

Randomize::sequence()->chars()->alphaUpperCase()->count(20)->asString()->generate();

Sometimes you want to obtain some random sequences with no duplicates. For example, you want to play "Tombola" (extracting numbers from 1 to 90 with NO duplicates):

$randomTombola = Randomize::sequence()->min(1)->max(90)->count(90)->noDuplicates()->generate();

Sometimes you want to obtain some random char sequences with no duplicates.

$randomChars = Randomize::sequence()->chars()->count(10)->noDuplicates()->generate();

Or you might want numeric char sequences with no duplicates. For example, char sequences of length 10:

$randomChars = Randomize::sequence()->chars()->numeric()->count(10)->noDuplicates()->generate();

Or you might want alphabetical char sequences with no duplicates.

$randomChars = Randomize::sequence()->chars()->alpha()->count(10)->noDuplicates()->generate();

Yes, even both and with no duplicates.

$randomChars = Randomize::sequence()->chars()->alphanumeric()->count(10)->noDuplicates()->generate();

Random String

If you want to generate a random string (alphanumeric, only letters, only numbers, with no duplication, lower case, upper case etc) you can use random chars generation. To generate a string use Randomize::chars():

$string = Randomize::chars()->generate();

The default behavior is:

  • 10 chars;
  • only letters (a-z);
  • lowercase;
  • duplication is allowed (the string 'aba' has a duplication for 'a' char).

You can change the default values with some methods and parameters. Method to set the type of chars (numeric, alpha, alphanumeric...):

  • alpha() : 'a-z', 'A-Z';
  • alphanumeric() : 'a-z', 'A-Z', '0-9';
  • numeric() : '0-9';
  • alphaLowerCase() : 'a-z';
  • alphaUpperCase() : 'A-Z';
  • specialCharacters() : !"#$%&'()*+,-./:;<=>?@[]^_`{|}~

You have also some methods to "control" the output, for example, avoid duplications:

  • unique(): it generates a string with at most one occurrence per character.

String with numeric chars ('0'-'9')

If you want to generate a string with 16 chars, made with numeric chars ('0'-'9'):

$string = Randomize::chars(16)->numeric()->generate();

You could obtain something like this '3963233500573002'.

String with 20 chars, lower case, just letters, no dups

If you want to obtain a string with 20 chars, lower case and just letters ('a'-'z'), and you want to avoid character duplications ( alphaLowerCase() and unique() ):

$string = Randomize::chars(20)->alphaLowerCase()->unique()->generate();

You could obtain something like this: 'nmbsjhrgdyfxwoltqkzp'.

String with 20 chars, letters and special characters

If you want to obtain a string with 20 chars, with letters and special characters:

$string = Randomize::chars(20)->specialCharacters()->alpha()->generate();

You could obtain something like this: 'IOgPckeGGifrD%DRy[*!'.

Draw random stuff

If you have a list of values and you want to extract/select/draw one or more elements, you could use the Draw class instead of Randomize. For using the Draw class you need to import it:

use HiFolks\RandoPhp\Draw;

Suggest which programming language you could use in your next project (random)

use HiFolks\RandoPhp\Draw;

$randomLanguage = Draw::sample(["PHP", "Python", "Golang", "Javascript"])->snap();
var_dump($randomLanguage);

Suggest which JS framework you could use in your next project (random)

$array=["React.js", "Vue.js", "Svelte.js", "Angular.js" , "Alpine.js", "Vanilla js"];
$randomJs = Draw::sample($array)->extract();

Extract 3 JS frameworks you could use in your next project

$array=["React.js", "Vue.js", "Svelte.js", "Angular.js" , "Alpine.js", "Vanilla js"];
$randomJs = Draw::sample($array)->count(3)->extract();

Extract 3 JS frameworks (duplicates allowed)

$array = ["React.js", "Vue.js", "Svelte.js", "Angular.js", "Alpine.js", "Vanilla js"];
$randomJs = Draw::sample($array)->count(3)->allowDuplicates()->extract();

Testing

composer test

If you want to see some coverage reports you can execute phpunit command with coverage-text option:

vendor/bin/phpunit --coverage-text

Warning ⚠️

Under the hood, RandoPHP uses some native PHP functions like:

These PHP functions use a pseudo-random number generator that is not suitable for cryptographic purposes.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Submit ideas or feature requests or issues

Take a look if your request is already created https://github.com/Hi-Folks/rando-php/issues If it is not present, you can create a new one https://github.com/Hi-Folks/rando-php/issues/new

Credits

License

The MIT License (MIT). Please see License File for more information.

About

RandoPhp is a open source library that implements random generators (Integer, Char, Byte, Sequences, Boolean) and take random sample from arrays

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages