Solved

Random number using RandRange

Posted on 2008-10-30
6
4,960 Views
Last Modified: 2013-12-24
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")#>

Open in new window

0
Comment
Question by:Janrow
  • 3
  • 2
6 Comments
 
LVL 18

Accepted Solution

by:
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

by:azadisaryev
azadisaryev earned 250 total points
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

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

<cfset myNumber = randRange(1, 999) />
<cfoutput>#numberFormat(myNumber, "0000")#</cfoutput>
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:Janrow
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#" />

Open in new window

0
 
LVL 18

Expert Comment

by:Plucka
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

by:Janrow
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#'

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question