Random numbers and alphabet mixture

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jumperbl
    New Member
    • Mar 2008
    • 17

    #1

    Random numbers and alphabet mixture

    Hi,

    I am a noob and I was wondering if there is way to generate random mixture of alphabets and numbers? For example:

    1234567890ABCDE FG
    or
    4BA92ABD293BC38 90

    the length needs to be 17 characters long.
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    Originally posted by jumperbl
    Hi,

    I am a noob and I was wondering if there is way to generate random mixture of alphabets and numbers? For example:

    1234567890ABCDE FG
    or
    4BA92ABD293BC38 90

    the length needs to be 17 characters long.
    How about this list comprehension:
    [code=python]
    >>> ''.join([chr([random.randrang e(48,58),random .randrange(65,9 1)][random.randint( 0,1)]) for x in xrange(17)])
    '3E4F8F756F584Z 8XU'
    [/code]

    Or in non-comprehension form:
    [code=python]
    import random

    rstring = ''
    for x in xrange(17):
    rint = random.randint( 0,1)
    if rint:
    rstring += chr(random.rand range(48,58))
    else:
    rstring += chr(random.rand range(65,91))

    >>> rstring
    'T374TX07KEP031 F17'
    [/code]

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by jumperbl
      Hi,

      I am a noob and I was wondering if there is way to generate random mixture of alphabets and numbers? For example:

      1234567890ABCDE FG
      or
      4BA92ABD293BC38 90

      the length needs to be 17 characters long.
      That is easily done with the random.choice() function, a list or string of character choices, and a list comprehension or for loop. Select a character from the choices 17 times, and join or concatenate the characters. Try coding yourself and post back.

      Example output from a variable characters, 10 random strings:

      >>> characters
      'abcdefghijklmn opqrstuvwxyzABC DEFGHIJKLMNOPQR STUVWXYZ0123456 789'
      >>> j5jSeP4etag0Euc N0
      BAlxpfUfXnEjBNa L6
      dB0GSaCFZMRg2Dr qK
      uMLz8ivN3cm8k8M CC
      b6qHuPAf7ATYsLI jd
      cfYmLEfGa66nKZz lJ
      ldQd3ouLNSG7ghS u3
      vxFn9UHchY8nRe2 Mj
      QfbuEMtfjRZJuxA LZ
      18TcORlqnZGrOUU rm
      >>>

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Now that we have some code in this thread, I guess I'll post mine :)
        [code=Python]>>> import random
        >>> import string
        >>> characters = string.letters+ string.digits
        >>> no_chrs = 17
        >>> print ''.join([random.choice(c haracters) for _ in range(no_chrs)])
        uGAGd5Er82VOFsj wh
        >>> [/code]

        Comment

        • jumperbl
          New Member
          • Mar 2008
          • 17

          #5
          thanks guys for the help.

          Comment

          • jumperbl
            New Member
            • Mar 2008
            • 17

            #6
            [QUOTE=jlm699]How about this list comprehension:
            [code=python]
            >>> ''.join([chr([random.randrang e(48,58),random .randrange(65,9 1)][random.randint( 0,1)]) for x in xrange(17)])
            '3E4F8F756F584Z 8XU'
            [/code]

            I am trying to understand this. So, you have a chr range of 0-9[48,58] and A-Z[65-91], but what is random.randint( 0,1) is that step by one? I don't understand this. I like how this is written, but hopefully someone can explain.

            Comment

            • jlm699
              Contributor
              • Jul 2007
              • 314

              #7
              Originally posted by jumperbl
              what is random.randint( 0,1) is that step by one? I don't understand this.
              No that is an obfuscated way to randomly choose between one of two choices.
              Let's say you have a list tst = ['a', 'b']
              Element 0 (tst[0]) is 'a' and element 1 (tst[1]) is 'b'. So let's say instead of storing the list to a variable tst you simply use the list itself:
              [code=python]
              >>> ['a', 'b'][0]
              'a'
              >>> ['a', 'b'][1]
              'b'
              [/code]
              Now if we put random.randint( 0,1) in place of the index specifier, we have a randomly chosen element from the list picked
              [code=[python]
              >>> import random
              >>> ['a', 'b'][random.randint( 0,1)]
              'b'
              >>> ['a', 'b'][random.randint( 0,1)]
              'a'
              >>> [/code]
              So in my code example I have the random letter and the random number pickers contained in a ad-hoc list [random_letter, random_number] and an element is chosen based on the result of randint(0,1). Does that clear it up a little bit? Unfortunately, this is not the best practice in terms of computing efficiency as a random letter and two random numbers are generated on each iteration (actually three random numbers), whereas bvdet's solution only requires one random number (index of list) to be generated.

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                Originally posted by jumperbl
                Originally posted by jlm699
                How about this list comprehension:
                [code=python]
                >>> ''.join([chr([random.randrang e(48,58),random .randrange(65,9 1)][random.randint( 0,1)]) for x in xrange(17)])
                '3E4F8F756F584Z 8XU'
                [/code]
                I am trying to understand this. So, you have a chr range of 0-9[48,58] and A-Z[65-91], but what is random.randint( 0,1) is that step by one? I don't understand this. I like how this is written, but hopefully someone can explain.
                Breaking it up into two parts:
                [code=Python]
                >>> (chr(random.ran drange(48,58)), chr(random.rand range(65,91)))
                ('6', 'O')
                >>> (chr(random.ran drange(48,58)), chr(random.rand range(65,91)))
                ('8', 'Y')
                >>> random.randint( 0,1)
                0
                >>> random.randint( 0,1)
                1
                >>> (chr(random.ran drange(48,58)), chr(random.rand range(65,91)))[random.randint( 0,1)]
                'T'
                >>> (chr(random.ran drange(48,58)), chr(random.rand range(65,91)))[random.randint( 0,1)]
                'R'
                >>> (chr(random.ran drange(48,58)), chr(random.rand range(65,91)))[random.randint( 0,1)]
                '3'
                >>> [/code]#1 - Create a tuple containing a letter and a number
                #2 - Take a slice of the tuple (using random.randint( 0,1)) to produce a random letter or number

                Comment

                • jumperbl
                  New Member
                  • Mar 2008
                  • 17

                  #9
                  Thanks that helps. Makes sense now.

                  Originally posted by jlm699
                  No that is an obfuscated way to randomly choose between one of two choices.
                  Let's say you have a list tst = ['a', 'b']
                  Element 0 (tst[0]) is 'a' and element 1 (tst[1]) is 'b'. So let's say instead of storing the list to a variable tst you simply use the list itself:
                  [code=python]
                  >>> ['a', 'b'][0]
                  'a'
                  >>> ['a', 'b'][1]
                  'b'
                  [/code]
                  Now if we put random.randint( 0,1) in place of the index specifier, we have a randomly chosen element from the list picked
                  [code=[python]
                  >>> import random
                  >>> ['a', 'b'][random.randint( 0,1)]
                  'b'
                  >>> ['a', 'b'][random.randint( 0,1)]
                  'a'
                  >>> [/code]
                  So in my code example I have the random letter and the random number pickers contained in a ad-hoc list [random_letter, random_number] and an element is chosen based on the result of randint(0,1). Does that clear it up a little bit? Unfortunately, this is not the best practice in terms of computing efficiency as a random letter and two random numbers are generated on each iteration (actually three random numbers), whereas bvdet's solution only requires one random number (index of list) to be generated.

                  Comment

                  Working...