CodeIgniter Forums
How random is the random string function? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How random is the random string function? (/showthread.php?tid=58358)



How random is the random string function? - El Forum - 06-06-2013

[eluser]ShoeLace1291[/eluser]
I use the random string function to generate activation codes for my members' accounts. What are the chances that this function will generate the same string twice?


How random is the random string function? - El Forum - 06-06-2013

[eluser]LuckyFella73[/eluser]
There are different functions in PHP generating random results. Which one
do you use?

I would prefer the "mt_rand()" function over "rand()".

http://php.net/manual/de/function.mt-rand.php


How random is the random string function? - El Forum - 06-06-2013

[eluser]boltsabre[/eluser]
If you are really worried about this, and it is absolutely critical that activation codes are unique can also append/prepend a unix timestamp to your random string when you create it, virtually impossible for duplication if your random string in 10+ characters.


How random is the random string function? - El Forum - 06-28-2013

[eluser]niki_mihaylov[/eluser]
Just use user ID from the database - this way there is no chance of getting same code twice. Also you can add timestamp if generating more codes for same user in future. Then you can Md5 or use other sexy function to serve it Smile


How random is the random string function? - El Forum - 06-28-2013

[eluser]Pert[/eluser]
You can use PHP uniqid()