11 Replies Latest reply on Jul 20, 2009 7:24 AM by TLC-IT

    random numbers

    JohnGree Level 1
      Hi i did have some code that made up a alfanumeric random number

      i know it took from a list of abcdefghijklmnopqrstu0123456789

      anyway i have lost it and i need a random afla-numeric does anyone know the code to do this?
        • 1. Re: random numbers
          Dan Bracuk Level 5
          Generate a random number and convert it to hex.
          • 2. Re: random numbers
            Level 7
            > Generate a random number and convert it to hex.

            That's not going to help with the G-Z part of the alphabet.

            To the OP: why are you trying to do this?

            To answer your original question:
            1) create a list of valid characters, like you have
            2) select a random number between 1 and [length of that list]
            3) get the character from the list @ that position and store it in your
            result string
            4) repeat 2-3 until you have a string of the desired length.

            --
            Adam
            • 3. Re: random numbers
              insuractive Level 3
              Hex is a good call, as it will prevent any *UNFORTUNATE* letter combinations
              • 4. Re: random numbers
                JohnGree Level 1
                hi Adam, that sound like what i had before, but what is the code to do this?

                • 5. Re: random numbers
                  Dan Bracuk Level 5
                  quote:

                  Originally posted by: JohnGree
                  hi Adam, that sound like what i had before, but what is the code to do this?



                  The functions randrange and listgetat, combined with the concatonation operator should work.
                  • 6. Re: random numbers
                    Level 7
                    > hi Adam, that sound like what i had before, but what is the code to do this?

                    Sorry mate, I rarely provide code for solutions that really oughtn't be too
                    far from the grasp of the person needing it. I'm a firm believer in the
                    "teach a person to fish" approach to problem solving. Call me a b@st@rd if
                    you like.

                    Read the docs.

                    Or post your code here, and we can go through where it could use
                    modification.

                    --
                    Adam
                    • 7. Re: random numbers
                      BKBK Adobe Community Professional & MVP
                      The code

                      <cfset rndm = lcase(replace(CreateUUID(), "-", "", "all"))>
                      <cfoutput>#rndm#</cfoutput>

                      will generate a random string of 32 characters from the list a,b,c,...,f,0,1,2,...,9. You can apply a function like left(), right() and so on, to obtain a shorter string.





                      • 8. Re: random numbers
                        Level 7
                        is this the code you have 'lost'?:
                        http://www.cflib.org/udf.cfm?ID=529

                        --

                        Azadi Saryev
                        Sabai-dee.com
                        http://www.sabai-dee.com
                        • 9. Re: random numbers
                          dalcde Level 1

                          You can make it super complicated:

                          number = Math.random()*35

                          if(number ==10){

                               number = a

                          } else if(number ==11){

                               number = b

                          } else if(number ==12){

                               number =c

                          } else if(number ==13){

                               number =d

                          } else if(number ==14){

                               number =e

                          } else if(number ==15){

                               number =f

                          }

                          etc.

                          It would be super long, but it works.( number is the name of a variable )

                          • 10. Re: random numbers
                            dalcde Level 1

                            This code is also OK ( It seems that you just want a - u, change the code if you like)

                                    list = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");

                                    character = list[Math.round(Math.random()*35];

                             

                            This code is a lot more simple!

                            • 11. Re: random numbers
                              TLC-IT Level 3

                              If you have a list of letters to draw from, and don't mind if letters are repeated, then your algorithm (easily implemented in just a few lines of code) looks something like this:  (don't ask me to write it...)

                               

                              1. Initialize the result-variable to an empty string.
                              2. Repeat the following for as many characters as you need...
                                1. Select a random index in the range 0 <= n < character_list_size by calculating Trunc( Rand() * character_list_size ) where Trunc is a function that truncates to an integer and Rand is a function that returns a random floating-point number 0 <= n < 1.0.
                                2. Append {the character in the list at that position} to the result.