Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random numbers

Status
Not open for further replies.

iao

Programmer
Feb 23, 2001
111
US
I am trying to create a form so that when the user access the page, this number is randomly created and displayed to the user. This number must be unique as well. So, not only do I need a random number, but it must be a random number that does not currently exist in the database.

Here is my code to generate the random number:
Code:
<CFQUERY NAME=&quot;qNUMBER&quot;>
	SELECT NUMBER
	FROM TS.NUMBER
</CFQUERY>

<CFSET tempnum = #randrange(1, 10)#>
<CFLOOP CONDITION=&quot;qNUMBER.NUMBER DOES NOT CONTAIN #tempnum#&quot;>
<CFSET tempnum = tempnum>
<CFBREAK>
</CFLOOP>

<CFOUTPUT>#tempnum#</CFOUTPUT>

I am probably going about this all wrong. Any ideas???

Any help would be much appreciated. Thanks!
 
You want to use CreateUUID:

<CFIF IsDefined(&quot;Form.CreateUUID&quot;) Is True>
<P>Your new UUID is: <CFOUTPUT>#CreateUUID()#</CFOUTPUT></P>
</CFIF>

<FORM ACTION=&quot;createuuid.cfm&quot; METHOD=&quot;post&quot;>

<P><INPUT TYPE=&quot;Submit&quot; NAME=&quot;CreateUUID&quot;> </P>
</FORM>
John Hoarty
jhoarty@quickestore.com
 
Hi JHoarty;

Thanks for your reply. I have never heard of CreateUUID before. I did a search in CF studio and read the help file. However, I don't know if you can answer this but how can this value be set to a maximum of 6 digits. This field can only be 6 numeric digits long. And, it has to exist when the user enters the form.
 
Aha,

Can't format UUID as far as I know. You were on the right track, though, for what you wanted to do. Try this:

<cfset flag = 'dupInt'>

<cfloop condition=&quot;flag EQ 'dupInt'&quot;>

<cfset randInt = #randrange(1, 100000)#>

<cfquery datasource=&quot;#datasource#&quot; name=&quot;getRand&quot;>
SELECT 1 FROM NumTable
WHERE Number = #randInt#
</cfquery>

<cfif #getRand.RecordCount# EQ 0>
<cfset flag = 'uniqueInt'>
</cfif>

</cfloop>

<cfquery datasource=&quot;#datasource#&quot;>
INSERT INTO NumTable
VALUES(#randInt#)
</cfquery>


<cfoutput>
Your random integer is #randInt#
</cfoutput>
John Hoarty
jhoarty@quickestore.com
 
Does it HAVE to be random or can it simply be unique? If what you're looking for is a unique number then try something like this:

year month day hour minute second = 010726105123

Now six digits would be a little less to work with but you could perhaps leave out some of the fields (like year) to get a shorter number.

It's perhaps one of the fastest solutions I know to get a unique number without going overboard.

-LN
 
Don't know if this is the best way to go, but here is what I have and it works.

<CFSET tempnum = &quot;0&quot;>
<!--- Loop until tempnum doesn't equal to 0 --->
<CFLOOP CONDITION=&quot;#tempnum# EQ 0&quot;>
<CFSET tempnum = #randrange(100000, 600000)#>
<CFQUERY NAME=&quot;qno&quot;>
SELECT no
FROM TS.no
WHERE no = #tempnum#
</CFQUERY>
<CFIF qno.RecordCount EQ 0>
<CFBREAK>
<CFELSE>
<CFSET tempnum = &quot;0&quot;>
</CFIF>
</CFLOOP>
<BR>
tempnum:<CFOUTPUT>#tempnum#</CFOUTPUT>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top