Welcome to Dream.In.Code
Click Here
Getting Help is Easy!

Join 117,542 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,673 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



The help I looked for Random Strings

 
Reply to this topicStart new topic

The help I looked for Random Strings, This code makes random strings

rhoisy
post 3 Dec, 2007 - 12:02 PM
Post #1


New D.I.C Head

*
Joined: 19 Nov, 2007
Posts: 5


My Contributions


I hope this code is useful to someone; I was trying to do something and found I did not understand what I was being told about conditional loops, so I built this so I could see how they worked. If it helps you great, if not sorry.
CODE

<!--- This must be some pretty poor code written my rhoisy
Today is Dec 03 2007
If some one wants to make this code better and easier to understand, be my guest, just send me a copy so I can analyse my mistakes. --->
<!--- Create a ColdFusion page with the following content: --->
<cfset chars = "0123456789abcdefghiklmnopqrstuvwxyz">
<cfset SpecialChar = "##$%^&()">
<cfset strLength = 7>
<cfset r = 0>
<!--- <cfset j = 0> --->
<cfset new_passwords = 25>
<cfset passwords = 1>
<cfset no_special_char = 0>
<cfset randout = ArrayNew(1)>


    <!--- Setting the Array up for the correct number of new passwords according to #new_passwords# --->
    <cfloop from="1" to="#new_passwords#" index="b">
        <cfset randout[b] = "">
    </cfloop>
<!--- Look and see what is in the Array --->
<!--- Double loop - First loop divides the random strings; while the
second loop  divides them into 6 letters and numbers --->

<!--- <cfloop from="1" to="#new_passwords#" index="j" > --->
<cfloop condition="#new_passwords# GE #passwords#" >
    <!--- Set no special character to 0 so I can see if one gets set. --->
    <cfset no_special_char = 0>

    <!--- Inside loop make up the individual passwords --->
    <cfloop from="1" to="#strLength#" index="i">
        <!--- Set up randon number and random Special Character for the first pass --->
        <cfset rnum = ceiling(rand() * len(chars))>
        <cfset rspecial = ceiling(rand() * len(SpecialChar))>

        <cfif rnum EQ 0 >
            <cfset rnum = 1>
        </cfif>
        
        <!--- Looking for a randon match to a randon number/MOD of a randon number or just a randon number, How dumb can I get? --->
        <cfif rnum is #i# >
            <cfset randout[#passwords#] = randout[#passwords#] & mid(SpecialChar, rspecial, 1)>
            <cfset #no_special_char# = #no_special_char# + 1>
            <!--- This number could have MODs attached and we need to consider them as well --->
        <cfelseif rnum Mod 7 is #i# >
            <cfset randout[#passwords#] = randout[#passwords#] & mid(SpecialChar, rspecial, 1)>
            <cfset #no_special_char# = #no_special_char# + 1>
        <cfelse>
            <cfset randout[#passwords#] = randout[#passwords#] & mid(chars, rnum, 1)>
        </cfif>
            <!--- I'm leaving these cf dumps as they helped me find the solution when I had not clue how this code worked.
            This is the state of randout during the first loop <cfdump var="#randout[passwords]#"> <P>
            This is the password count: <cfdump var="#passwords#"> <P>
            <!--- This is false Special Char:<cfdump var="#i#"> <P> --->
            This is the state of the no special Character after rounout <cfdump var="#no_special_char#"> <P>
            This is the loop I count: <cfdump var="#i#"> <P> --->
            <!--- End of inside loop for seven character password --->                    
    </cfloop>
    <!--- once again this is juse information on how to use the different version of cf dump. --->
    <!--- This is the no special char outside of loop i <cfdump var="#no_special_char#"> --->
    <!--- This is the passwords count ourside of loop i <cfdump var="#passwords#">
    <!--- <cfdump var="#j#" >  is the J before the if <P> --->
    This is the current randoout; <cfdump var="#randout[passwords]#"> <P>
    --->

    <cfif no_special_char EQ 0>
        <cfset randout[passwords] = "">
    <cfelse>
        <cfset #passwords# = #passwords# +1>    
    </cfif>
      
</cfloop>

<html>
<head>
<title>Random String/Passwords</title>
</head>
<body>
In this piece of code you will see how I created a random string of letters, digits and special characters. This can be used for making random passwords. <P>
Althought, I can't understand why, but I really did it just to see how the loops could be made to do what I wanted them to do. <P>
<B> What made it important to me was the way the conditional loop works. <P> </B>
PS. If your random generator comes up with an "&" followed by a "#" and then a number you get "" and that one is hard to use as a password.
To create a simple randon string: <P>
<!--- This outputs the randon strings --->
<cfoutput>
    <!--- <cfloop from="1" to="Randout Length" index="k"> may work, but what is below does work.--->
    <cfloop from="1" to="#ArrayLen(randout)#" index="k">
    <!--- More debug and as you can see I had a lot of problems <cfdump var="#ArrayLen(randout)#"> <space> --->
    <strong> Index: #k# Random output: #randout[k]# </strong><P>
    </cfloop>
</cfoutput>
</body>
</html>
User is offlineProfile CardPM

Go to the top of the page


skyhawk133
post 3 Dec, 2007 - 12:09 PM
Post #2


Head DIC Head

Group Icon
Joined: 17 Mar, 2001
Posts: 14,600



Thanked 39 times

Dream Kudos: 1650

Expert In: Web Development

My Contributions


I moved this out of tutorials because it really doesn't fit the guidelines of a tutorial.

Glad you got it working though!
User is offlineProfile CardPM

Go to the top of the page

rhoisy
post 6 Dec, 2007 - 09:24 AM
Post #3


New D.I.C Head

*
Joined: 19 Nov, 2007
Posts: 5


My Contributions


QUOTE(skyhawk133 @ 3 Dec, 2007 - 02:09 PM) *

I moved this out of tutorials because it really doesn't fit the guidelines of a tutorial.

Glad you got it working though!

Thanks for taking care of me. I thought Tut was a good place. If I do this again, this is the correct place then? I can see this was not a real Tut. And yea I was glad it worked, too.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/7/08 05:32PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month