Solved

Random number using RandRange

Posted on 2008-10-30
6
4,927 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now