Unique Random Number using ColdFusion Function RAND()
Filed under ColdFusion
I've been playing with the RAND() function, trying to generate a unique random number so that I can use that random number as a sort of key to send the user, to confirm their email address.
I read on this forum HouseOfFusion that an easy way to create a unique random number is to create a 9 digit random number with the RAND() Function and then take today's date and today's time, format it to a number, followed by another 9 digit number using the RAND() function. Add all 3 together and you got a number that is nearly impossible to duplicate. Here is the code to make that happen.
<!--- Create a Random number to be used as email verification --->
<!--- Assign the current date and time to a variable. --->
<cfset tdstamp="#DateFormat(Now(),'mmddyyyy')##TimeFormat(Now(),'hhmmss')#">
<!--- Create a random number. --->
<cfset randomnum1=RAND()*100000000>
<!--- Create another random number. --->
<cfset randomnum2=RAND()*100000000>
<!--- Concatenate the first random number, the time and date stamp, and the second random number. --->
<cfset uniquenumber="#randomnum1##tdstamp##randomnum2#">
Although this number is nearly impossible to be recreated, is there another way to actually create a truly random number and make that number never show up again?
My first thought is to simply generate that number, insert it into the database. Then next time a unique random number is generated, query the database to see if that number has ever been created. But what if you are not storing the number anywhere, how could you generate a truly unique randomly generated number?
If anyone has any suggestions, I would love to hear them.
Thanks in advance.
| View count: 6366
Nov6









