Generate random number in Laravel

If you want to generate the random string like you said, replace:

$string = str_random(15);

with

// Available alpha caracters
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

// generate a pin based on 2 * 7 digits + a random character
$pin = mt_rand(1000000, 9999999)
    . mt_rand(1000000, 9999999)
    . $characters[rand(0, strlen($characters) - 1)];

// shuffle the result
$string = str_shuffle($pin);

Edit:

Before, the code wasn't generating a random alpha character all the time. Thats because Laravel's str_random generates a random alpha-numeric string, and sometimes that function returned a numeric value (see docs).


This should give you random number between 2 & 50.

$randomId       =   rand(2,50);