Solved

Numbers and strings

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
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 …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 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

13 Experts available now in Live!

Get 1:1 Help Now