Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

random string generator algorithm

<?php
declare(strict_types=1);

function randomStrGenerator(int $length): string
{
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';

    if ($length < 5) {
        return 'inputed value  must be greater than 4';
    } else {
        for ($i = 0; $i < $length; $i++) {
            $index = rand(0, strlen($characters) - 1);
            $randomString .= $characters[$index];
        }

        return $randomString;
    }
}

echo "Error : ".randomStrGenerator(4)."
"; //This will throw an error with what the function expects

echo "Length of 5: ".randomStrGenerator(5)."
"; // This will give us a random length of 5 strings

echo "Length of 10: ".randomStrGenerator(10)."
";//This will give us a random length of 10 strings

echo "Length of 20: ".randomStrGenerator(20)."
";//This will give us a random length of 20 strings


echo "Hope you find this helpful";
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #random #string #generator #algorithm
ADD COMMENT
Topic
Name
1+4 =