Solved

Random number using RandRange

Posted on 2008-10-30
6
4,983 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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 …
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…

820 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