str_random (Str::random()) tries to use openssl_random_pseudo_bytes which is a pseudo random number generator optimized for cryptography, not uniqueness. If openssl_random_pseudo_bytes is not available, it falls back to quickRandom():

public static function quickRandom($length = 16)
{
    $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

    return substr(str_shuffle(str_repeat($pool, 5)), 0, $length);
}

In my opinion quickRandom code is not reliable for uniqueness nor cryptography.

Yes, having openssl_random_pseudo_bytes and using 32 bytes is almost impossible to see a collision, but it's still possible. If you want to make sure your strings/numbers will be unique (99.99%), you better use a UUID function. This is what I normally use:

/**
 * 
 * Generate v4 UUID
 * 
 * Version 4 UUIDs are pseudo-random.
 */
public static function v4() 
{
    return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',

    // 32 bits for "time_low"
    mt_rand(0, 0xffff), mt_rand(0, 0xffff),

    // 16 bits for "time_mid"
    mt_rand(0, 0xffff),

    // 16 bits for "time_hi_and_version",
    // four most significant bits holds version number 4
    mt_rand(0, 0x0fff) | 0x4000,

    // 16 bits, 8 bits for "clk_seq_hi_res",
    // 8 bits for "clk_seq_low",
    // two most significant bits holds zero and one for variant DCE1.1
    mt_rand(0, 0x3fff) | 0x8000,

    // 48 bits for "node"
    mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
    );
}

It generates a VALID RFC 4211 COMPLIANT version 4 UUID.

Check this: https://en.wikipedia.org/wiki/Universally_unique_identifier#Collisions

Answer from Antonio Carlos Ribeiro on Stack Overflow
Top answer
1 of 7
87

str_random (Str::random()) tries to use openssl_random_pseudo_bytes which is a pseudo random number generator optimized for cryptography, not uniqueness. If openssl_random_pseudo_bytes is not available, it falls back to quickRandom():

public static function quickRandom($length = 16)
{
    $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

    return substr(str_shuffle(str_repeat($pool, 5)), 0, $length);
}

In my opinion quickRandom code is not reliable for uniqueness nor cryptography.

Yes, having openssl_random_pseudo_bytes and using 32 bytes is almost impossible to see a collision, but it's still possible. If you want to make sure your strings/numbers will be unique (99.99%), you better use a UUID function. This is what I normally use:

/**
 * 
 * Generate v4 UUID
 * 
 * Version 4 UUIDs are pseudo-random.
 */
public static function v4() 
{
    return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',

    // 32 bits for "time_low"
    mt_rand(0, 0xffff), mt_rand(0, 0xffff),

    // 16 bits for "time_mid"
    mt_rand(0, 0xffff),

    // 16 bits for "time_hi_and_version",
    // four most significant bits holds version number 4
    mt_rand(0, 0x0fff) | 0x4000,

    // 16 bits, 8 bits for "clk_seq_hi_res",
    // 8 bits for "clk_seq_low",
    // two most significant bits holds zero and one for variant DCE1.1
    mt_rand(0, 0x3fff) | 0x8000,

    // 48 bits for "node"
    mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
    );
}

It generates a VALID RFC 4211 COMPLIANT version 4 UUID.

Check this: https://en.wikipedia.org/wiki/Universally_unique_identifier#Collisions

2 of 7
75

you can use this

use Illuminate\Support\Str;

$random = Str::random(40);
🌐
Laravel Daily
laraveldaily.com › post › generate-random-strings-laravel-helper-methods
Generate Random Strings with Laravel: Helper Methods
May 22, 2024 - Laravel have a fake() helper which resolves to a Faker package. With the help of a faker, you can generate various things. The asciify() or regexify() methods can be used to generate a random string using faker.
🌐
Medium
medium.com › @harrisrafto › enhance-your-string-manipulation-with-str-random-in-laravel-ea3a3ac29f49
Enhance your string manipulation with Str::random() in Laravel | by Harris Raftopoulos | Medium
July 8, 2024 - The Str::random() function is part of the Illuminate\Support\Str class. It generates a random string of the specified length using a pool of alphanumeric characters. This is particularly useful for creating secure and unique tokens or identifiers.
🌐
YouTube
youtube.com › watch
Laravel: Get a Random String in 5+ Ways - YouTube
There are various helper methods in Laravel/PHP to generate random strings.Faker docs: https://fakerphp.github.io/- - - - -Support the channel by checking ou...
Published: July 1, 2024
🌐
Laravel News
laravel-news.com › home › laravel packages › the random package generates cryptographically secure random values
The Random package generates cryptographically secure random values
April 15, 2024 - $password = Random::dashed(int $length = 25, string $delimiter = '-', int $chunkLength = 5, bool $mixedCase = true): string; Securely shuffle an array, string, or Laravel Collection, optionally preserving the keys.
🌐
LinkedIn
linkedin.com › pulse › generating-random-strings-laravel-php-rabib-galib-llisc
Generating Random Strings in Laravel and PHP
Login to LinkedIn to keep in touch with people you know, share ideas, and build your career.
Find elsewhere
🌐
DEV Community
dev.to › aleson-franca › a-deep-dive-into-laravel-str-helper-methods-3oii
A Deep Dive into Laravel: Str Helper Methods - DEV Community
March 17, 2025 - The Str helper in Laravel is a powerful tool for string manipulation, offering practical and efficient solutions for everyday development needs. Whether you need to format, replace, truncate, or generate slugs and random strings, Str helps make ...
🌐
Laracasts
laracasts.com › discuss › channels › general-discussion › generate-unique-random-string
Generate unique random string
/** * Generate a more truly "random" alpha-numeric string. * * @param int $length * @return string * * @throws \RuntimeException */ public static function random($length = 16) { if ( !
🌐
Medium
medium.com › @randomstr › random-string-generation-in-php-and-its-frameworks-bb4b2f1bbce6
Random String Generation in PHP and Its Frameworks | by Random STR | Medium
December 1, 2023 - This function uses random_bytes, ... PHP framework, provides a more straightforward method to generate random strings using the Str helper:...
🌐
ItSolutionstuff
itsolutionstuff.com › post › how-to-generate-random-unique-string-in-laravel-5example.html
How to Generate Random Unique String in Laravel? - ItSolutionstuff.com
April 16, 2024 - Laravel provide several string helper that way we can use it easily like str_limit, str_plural, str_finish, str_singular etc. If you need to generate unique random string then you can use str_random() helper of Laravel.
🌐
Code And Deploy
codeanddeploy.com › home › blog › laravel › laravel str::random() helper function example
Laravel Str::random() Helper Function Example
May 28, 2022 - function random_string($length = 10) { $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $charactersLength = strlen($characters); $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, $charactersLength - 1)]; } return $randomString; } In Laravel, they provide a helper function to shorten our work and not write this function anymore.
🌐
Weblance-online
weblance-online.com › home › website › laravel › using laravel helper methods to generate random strings
Using Laravel Helper Methods To Generate Random Strings - Weblance Online Solutions
June 19, 2024 - The str_random() method uses PHP’s built-in random number generator to create the random string, ensuring that the generated string is truly random and secure. This makes it an ideal choice for generating passwords, tokens, or unique identifiers in your Laravel applications.
🌐
GitHub
github.com › seymuromarov › randomcrap-laravel
GitHub - seymuromarov/randomcrap-laravel: Laravel api for generating random strings,floats,integers,arrays and etc · GitHub
use Seymuromarov\Randomcrap\Facades\Randomcrap; //... Randomcrap::int(); //will return 6 length int value Randomcrap::string(); //will return 6 length string
Author: seymuromarov
Author: jorenvh