Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Numbers and strings

Posted on 2011-03-16
4
Medium Priority
?
410 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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Integration Management Part 2
Suggested Courses

971 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