kenprogrammer's avatar

Laravel Random String Generator: Is there a possibility of collision?

Is there a possibility of a same string being generated twice with below Laravel string helper?

$random = Str::random(40);

I want to create unique account codes

0 likes
2 replies
CorvS's avatar

Yes, but unlikely. You have 36 to the power of 40 possible (case insensitive) combinations. That's a very big number. Laravel uses Str::random(60) internally to create the "remember me" tokens for a user for example.

1 like
automica's avatar
automica
Best Answer
Level 54

@kenprogrammer being random, its entirely possible you might generate the same string again.

if you are issuing these to a user and are storing them in a table, you can generate your code, and check it against an existing code. if it matches, just generate another.

2 likes

Please or to participate in this conversation.