Solved

Numbers and strings

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

705 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

20 Experts available now in Live!

Get 1:1 Help Now