Coldfusion number generator using 4 digits, increment by 1

I need example of code that will generate a 4 digit number increment by 1.
Who is Participating?
gdemariaConnect With a Mentor Commented:
<cfset theNUmber = randRange(1000,9999)

will give you a random number

<cfset theNumber = 1000>

   incredment it by one...

<cfset theNumber = theNumber + 1>

  I have a feeling you want something more than that though...
smares32371Author Commented:
That's true, in order for me to get a unique incremental number for every time the page is loaded, do I have to insert the number into db table in order to keep track of the count.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

What is the number being used for? Just wondering if there's a reason you can't just use your database to generate the number ie (ms sql - identity, mySQL auto_increment, etc..). You could always pad the value on output so it's at leat 4 characters

     ie NumerFormat(yourNumber, "0000")
smares32371Author Commented:
invoice number
So far I don't see why you couldn't let your db generate the number. (In fact it's better not to roll your own when possible. As it's easy to create dupes.) Just pad the output as needed. If you prefer you could create a db view /or add a calculated column that does the padding automatically.

Which db type?
smares32371Author Commented:
I'm sorry it just has to be a 4 digit random number sorry for the incremental part of it.
Here's an example

1, '00001', '2011-09-26 00:00:00', 'Test invoice A'
2, '00002', '2011-09-26 00:00:00', 'Test invoice B'
3, '00003', '2011-09-26 00:00:00', 'Test invoice C'

Sample Structure
<!--- sample table --->
<!--- note: You may want to plan ahead and use something bigger than SMALLINT --->
invoiceID smallint auto_increment primary key,
invoiceDate datetime,
Notes varchar(255)

<!--- add sample data --->
INSERT INTO Invoices (invoiceDate, Notes)
SELECT curDate(), 'Test invoice A' union all
SELECT curDate(), 'Test invoice B' union all
SELECT curDate(), 'Test invoice C'

<!--- create one time VIEW that includes "InvoiceNum" column padded to 5 zeros --->
SELECT  InvoiceID, lpad(invoiceID, 5,'0') As InvoiceNum, invoiceDate, Notes
FROM   Invoices

<!--- display results --->
SELECT InvoiceID, InvoiceNum, InvoiceDate, Notes
FROM   vInvoices

Open in new window

> I'm sorry it just has to be a 4 digit random number sorry for the incremental part of it.

Oh, okay. Then ignore my last post
You could use randRange() to generate a random number. Use NumberFormat to pad it to 4 characters.

             ie   <cfset randInvoiceNum = numberFormat(randRange(0, 9999), "0000")>

But you'll have to loop to ensure it wasn't used already.  Create a unique constraint on the table to prevent dupes. Then use a try/catch to detect a constraint violation (ie the number was already used). Keep looping until the INSERT succeeds.

<!--- Use unique constraint to prevent dupes --->
invoiceID smallint auto_increment primary key,
invoiceNum char(4) NOT NULL,
UNIQUE KEY ukInvoices_InvoiceNum (invoiceNum))
> <cfset theNUmber = randRange(1000,9999)

Actually you can start at 0 or 1 and pad the results. So you don't skip the first 999 numbers.
@smares32371 - Glad you found an easy solution.  I have a feeling the functions by category view might come in handy in the future ;-)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.