?
Solved

Random number using RandRange

Posted on 2008-10-30
6
Medium Priority
?
5,106 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
[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
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 18

Accepted Solution

by:
Plucka earned 1000 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 1000 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
Turn your laptop into a mobile console!

The CV211 Laptop USB Console Adapter provides a direct Laptop-to-Computer connection for fast and easy remote desktop access with no software to install.

 

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
What You Need to Know when Searching for a Webhost Provider
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

765 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