1. Go to this page and download the library: Download savvot/random library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
savvot / random example snippets
//////// BASIC USAGE ////////
// Create xorshift random generator with default random seed
$rnd = new XorShiftRand();
// Default random value between 0 and GeneratorClass::INT_MAX
$value = $rnd->random();
// Random value within given range (inclusive)
$value = $rnd->random(15, 12333);
// Negative numbers allowed
$value = $rnd->random(-128, 128);
// Random unsigned int, size depends on underlying generator
$int = $rnd->randomInt();
// Random float in range 0 <= $val <= 1
$float = $rnd->randomFloat();
// True or false
$bool = $rnd->randomBool();
//////// STATE CONTROL ////////
// Read current seed
$seed = $rnd->getSeed();
// Set new seed, generator will be reinitialized
$rnd->setSeed('some new seed');
// .... several generator calls later ...
// Save state for future use
$state = $rnd->getState();
// .... several generator calls later ...
// Set saved state and restore generator to known state
$state = $rnd->setState($state);
// .... same calls to random methods as before will produce exactly the same output
// Reset generator to initial state with current seed ('some new seed')
$rnd->reset();
//////// STRING METHODS ////////
// Generate string with 256 random bytes
$bytes = $rnd->randomData(256);
// Generate random string from default (ALNUM) characters list with length = 17
$str = $rnd->randomString(17);
// Generate random string from numbers (0-9) with length = 99
$str = $rnd->randomString(99, $rnd::CL_NUM);
// Generate lowercase hex string with length = 32
$str = $rnd->randomString(32, $rnd::CL_HEXLC);
// Generate string from custom characters list
$str = $rnd->randomString(16, 'ABCDefgh#$%^');
// If custom list contains multi-byte characters, $mb flag must be set
$str = $rnd->randomString(16, 'АБВГДЕЁЖЗ', true);
//////// ARRAY METHODS ////////
// Get random key from array (0, 1, 2 or 'four')
$key = $rnd->arrayRand([10, 20, 30, 'four' => 40]);
// Get random value from array (10, 20, 30 or 4)
$value = $rnd->arrayRandValue([10, 20, 30, 'four' => 40]);
// Shuffle array. New numeric keys will be assigned
$array = [1, 2, 'key' => 3, 4, 'assoc' => 5, 6, 7];
$rnd->arrayShuffle($array);
// Shuffle array and maintain key => value associations
$array = [1, 2, 'key' => 3, 4, 'assoc' => 5, 6, 7];
$rnd->arrayShuffleAssoc($array);
//////// WEIGHTED RANDOM ////////
// Array in "key => weight" form must be specified
$array = ['one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, 'four2' => 4];
// Get random key from array by weights.
// So 'one' will appear less likely than 'four'\'four2'
$key = $rnd->arrayWeightRand($array);
// Shuffle array using weights.
// So 'four'\'four2' likely will appear earlier than 'one' in resulting array
$rnd->arrayWeightShuffle($array);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.