Solved

# Random number using RandRange

Posted on 2008-10-30
5,022 Views
I want to use RANDRANGE to generate a four digit number anywhere from 0001 to 9999. The problem is that the resultant number ends up less than four digits because, as an example, 0500 is reduced to 500 when displayed to the user as a variable and when saved to the database. Seems the leading 0 is removed.

I need four digits only. I realize I can set the random number 1000, 9999 but that does not feel right for the way  want to do it.

I doesn't seem to make much difference what data type I use in sql2000. init, smallinit, numeric, or varchar as I'm currently using. So, how do I solve this and what would be the best data type to save space in a large database?
``````<!--- These return only three resultant numbers if the number generated begins with a zero. --->

<cfset MYNUMBER = RandRange(0001, 9999)>
<!---or even --->
<cfset PRIZE1 = #numberFormat(randrange(0,9999), "0000")#>
``````
0
Question by:Janrow
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2

LVL 18

Accepted Solution

Plucka earned 250 total points
ID: 22838699
In your database you can keep it as a number.

ie  12

But when you display it, just format it

numberFormat(myNumber, "9999")
0

LVL 27

Assisted Solution

ID: 22838779
or you can set your column to char(4) or similar type.

plus, for the number generation, use randrange() as you do.
since it generates a NUMBER, not a string, it will drop any leading 0. so when you display the generated number to the user, do it like this:
<cfoutput>#repeatstring('0', 4-len(number)) & number#</cfoutput>

if you do store this number in your db as a string, then do same when you insert the data.
but be aware that string them as string instead of number may very well screw up any sorting/ordering you may want to do on them... many databases have functions to properly sort numeric strings, so you may have to look into that in this case...
0

LVL 18

Expert Comment

ID: 22838848
Sorry I got it wrong, use 0000 not 9999

<cfset myNumber = randRange(1, 999) />
<cfoutput>#numberFormat(myNumber, "0000")#</cfoutput>
0

Author Comment

ID: 22838880
Hmm. There must be a better way. This suggestions would mean tons of code revision and horribly complex. I'd rather forego my desire for the number to be from 1-9999 and make it 1000 - 9999, which should avoid the problem.
Also, without any modificatons, the resultant number variable does not display correctly in a FORM type TEXT as below, but does display correctly outside of a form as a simple cfoutput disapy of the variable.
I'll wait a bit to see if there are other suggestions, get some sleep and get back here. Thanks to you both. Later...

``````<input size="4" maxlength="4"  type="text" name="MYNUMBER" value="#PRIZE1#" />
``````
0

LVL 18

Expert Comment

ID: 22839103
It cant be done, unless its easier to change the database, change the database to a varchar(4) or char(4)

then you only need to generate the number formmatted once, and put it in the DB

<cfset myNumber = numberFormat(rangRange(1, 9999), "0000") />
0

Author Comment

ID: 22847756
Well, chicken guts!
It looks like the problem, all along, was my failure to set the variable PNUMBER in quotes during the query update as in the code below.
I was thinking it was a number so no quotes were necessary. My mistake. But I learned a couple things from you both so I'll split the points.
My database structure can be easily changed. So, with that in mind what would be the best data type for these numbers? It'll always be four numbers and I will not need to search or compare. How about smallinit instead of char or varchr? That way I know I always have numbers there.

``````<cfset pnumber = numberFormat(randRange(1, 9999), "0000") />
UPDATE PRECORDS
SET PNUMBER  = '#PNUMBER#',
WHERE ID = '#ID#'
``````
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

How to stress test an ASP.NET https website 3 101