Solved

Numbers and strings

Posted on 2011-03-16
4
406 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 500 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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

617 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