Download the PHP package hi-folks/rando-php without Composer
On this page you can find all versions of the php package hi-folks/rando-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package rando-php
Rando-PHP
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
- Usage, how to generate:
- Random char
- Random boolean
- Random float
- Random integer
- Random Bytes
- Random Date
- Random Sequences
- Random Chars
- Usage, how to extract random values from array/list
Installation
You can install the package via composer:
Usage
To import the right class:
Generate Char
Sometimes you want to obtain a random char, for example, a numeric char:
Or you might want an alphabetic char:
You can even do both!
You can generate a lower case char:
You can generate an upper case char:
You can generate a special character. A special character is one of '!"#$%&'()*+,-./:;<=>?@[]^_`{|}~':
Generate Boolean
Sometimes you want to obtain a random boolean true
or false
(flip a coin):
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:
Or you can set the min-max range of 0 - 90, which is equivalent to ->min(0)->max(90)
Generate an Integer
Sometimes you want to obtain a random integer (min-max range). For example, you want to roll the dice:
The same thing using range()
method, instead of min()
and max()
:
You can use a shortcut helper to generate an integer calling the constructor with min and max parameters:
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):
Generate a Date
Sometimes you want to obtain a random date. For example, you want to generate a random date:
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:
You can even specify your preferred format for the random date generated, by using the format()
method:
You can use min()
and max()
with format()
method:
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:
Sometimes you want to obtain some random char sequences. For example, char sequences of length 10:
Or you might want numeric char sequences.
Or you might want alphabetical char sequences.
Yes, even both.
You want 20 lowercase chars:
You want 20 upper case chars:
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):
Sometimes you want to obtain some random char sequences with no duplicates.
Or you might want numeric char sequences with no duplicates. For example, char sequences of length 10:
Or you might want alphabetical char sequences with no duplicates.
Yes, even both and with no duplicates.
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()
:
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'):
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()
):
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:
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:
Suggest which programming language you could use in your next project (random)
Suggest which JS framework you could use in your next project (random)
Extract 3 JS frameworks you could use in your next project
Extract 3 JS frameworks (duplicates allowed)
Testing
If you want to see some coverage reports you can execute phpunit
command with coverage-text
option:
Warning :warning:
Under the hood, RandoPHP uses some native PHP functions like:
- array_rand(): PHP Doc for array_rand;
- random_int(): PHP Doc for random_int;
- shuffle(): PHP Doc for shuffle;
- random_bytes(): PHP Doc for random_bytes.
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
- Roberto Butti
- All Contributors
- PHP Package Boilerplate
License
The MIT License (MIT). Please see License File for more information.