Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Numbers and strings

Posted on 2011-03-16
4
Medium Priority
?
407 Views
Last Modified: 2012-05-11
I have a number like 2635 that will be drawn from a db.  I need to create a random 4 digit number and have it put before the number.  I then need to put a different 4 digit number at the end.  So they would all be strung together like 374226358374.  

Then, I would need to break this number apart and isolate the original number.

Thanks
0
Comment
Question by:Rush_2112
[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
4 Comments
 
LVL 3

Accepted Solution

by:
sajayc earned 2000 total points
ID: 35152953
Hi,

This will just involve a couple of variables.

<cfset originalnum = query.number> <!--- This is the value from database  --->
<cfset randbefore = randrange(1000,9999)>
<cfset randafter = randrange(1000,9999)>
<cfset entirenumber = randbefore & originalnum & randafter>
<cfoutput>#entirenumber#</cfoutput>

The variables above will have everything seperate.

If you need to pull your orginal number out of the entire string, this code will do it.
<cfset extractnum = mid(entirenumber, 5, 4)> <!--- entirenum is the variable of the full number  --->
<cfoutput>#extractnum#</cfoutput>

0
 
LVL 52

Expert Comment

by:_agx_
ID: 35153411
(no points)

The technique above will probably be fine for your purposes.  But note RandRange generates pseudo random numbers. So if you have other requirements, like unique numbers, etc... that's something to keep in mind.

0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35153720
Another thing is that will your number be always of 4 digits like you have mentioned '2635' or the digits can change, this is because you need to get the number back,


if your number digit change, then you would need to store the number of digits in your orginal number somewhere and use the count in the code posted by @sajayc
<cfset originalNumber = '23456' />
<cfset count = len(originalNumber) />
<!--- ADD RANDOM NUMBERS --->
// code from @sajayc
<!-- EXTRACT , just add the count in there instead of 4 --->
<cfset extractnum = mid(entirenumber, 5, count)> <!--- entirenum is the variable of the full number  --->
<cfoutput>#extractnum#</cfoutput>

Also, as pointed by @_agx_ above, uniquness and randomness are 2 different things, unique is putting a restriction on random generation to have something not already generated.

0
 
LVL 1

Author Closing Comment

by:Rush_2112
ID: 35159426
Worked perfectly!  Thanks!  And thank you to everyone else who commented
0

Featured Post

Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

Question has a verified solution.

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

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 …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

722 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