PHP code example of ierusalim / php-random-gen

1. Go to this page and download the library: Download ierusalim/php-random-gen 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/ */

    

ierusalim / php-random-gen example snippets




$g = new RandomStr();

// Generate random 16 characters from default list
$str = $g->genRandomStr(16);
echo $str;

// Generate 33 random bytes
$bytes = $g->genRandomBytes(33);

// It works fast. No problem for create random string of million chars
$str = $g->genRandomStr(1000000);
echo "\nGenerated bytes: " . strlen($str);

// Need generate multibyte characters? No problems, set flag $utf8mode = true
$g->setChars("神會貓性少女 迪克和陰部", true);
$str = $g->genRandomStr(888);

// Generate string of 100 chars from character list "abcdefgh"
$g->setChars("abcdefgh");
$str = $g->genRandomStr(100);
echo $str;

// Generate string of 10 random words from specified words array
$words_arr = explode(',',' one, two, three, four, five, six, seven');
$g->setChars([$words_arr]);
echo $g->genRandomStr(10); 
// We get a result like " five three three three four seven four four six four"




$g = new RandomArray();

// Generate small random array with default parameters:
$arr = $g->genRandomArray();
print_r($arr);

// Generate random array with string keys from listed chars, 3-9 chars length
$g->setKeysModel(3, 9, 'abcdefghijklmnopqrstuvwxyz');
$g->setValuesModel(0, 100); //random numeric values range from 0 to 100
$arr = $g->genRandomArray(10, 15, 0); //generate 10-15 elements (not nested)
print_r($arr);




$g = new RandomArray();
$g->setKeysModel();     //set simple keys model (1,2,3,...n)
$g->setValuesModel();   //set simple values model (integer range 0-65535)
$lim_elements = 1000000;
$lim_depth = 3;
$_ts = microtime(true);
$arr = $g->genRandomArray(10000, 10000, 32768, $lim_depth, $lim_elements);
$total_generated = $lim_elements - $g->lim_elements;
echo (microtime(true) - $_ts) . " sec, generated: $total_generated elements.\n";

$g->setKeysModel();   //set simple keys model (1,2,3,...n)
$g->setValuesModel(); //set simple values model (integer range 0-65535)

$g->setKeysModel(min, max); //set keys model range from min to max (integer)
$g->setValuesModel(min, max); //set values model range from min to max (integer)

$g->setKeysModel(min_len, max_len, $char_str [, $utf8flag] )
$g->setValuesModel(min_len, max_len, $char_str [, $utf8flag] )

$g->setKeysModelFn(callable);
$g->setValuesModelFn(callable);



$g = new RandomArray();

// Keys are md5 of serial key numbers
$g->setKeysModelFn(function($parr) {
            \extract($parr); //$k, $v, $lim_depth
            return \md5($k);
        });
// Values - arrays with all parameters given for generation function
$g->setValuesModelFn(function($parr) { return $parr; });

$arr = $g->genRandomArray();

print_r($arr);
 bash
$ composer