Hacker News new | past | comments | ask | show | jobs | submit login

I wrote this simple Python function to create random strings from various character sets. I wish every NES game that had a password-based save used 'alpha_numeric_hardtoconfuse_lowercase'. There's no telling how many times I've mixed up 1, I, i, and l, pending on the crappy font being used. Same goes with hand-written Windows activation keys.

    # encoding: utf-8
    import random

    CHARACTER_SETS = {
        'alpha':                                                'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',             # 52
        'alpha_lowercase':                                      'abcdefghijklmnopqrstuvwxyz',                                       # 26
        'alpha_uppercase':                                      'ABCDEFGHIJKLMNOPQRSTUVWXYZ',                                       # 26
    
        'alpha_numeric':                                        'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',   # 62
        'alpha_numeric_lowercase':                              'abcdefghijklmnopqrstuvwxyz0123456789',                             # 36
        'alpha_numeric_uppercase':                              'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',                             # 36
    
        'alpha_hardtoconfuse':                                  'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ',                  # 47 --- Omits: i, l, o, I, O
        'alpha_hardtoconfuse_lowercase':                        'abcdefghjkmnpqrstuvwxyz',                                          # 23 --- Omits: i, l, o
        'alpha_hardtoconfuse_uppercase':                        'ABCDEFGHJKLMNPQRSTUVWXYZ',                                         # 24 --- Omits: I, O
    
        'alpha_numeric_hardtoconfuse':                          'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789',          # 55 --- Omits: i, l, o, I, O, 0, 1
        'alpha_numeric_hardtoconfuse_lowercase':                'abcdefghjkmnpqrstuvwxyz23456789',                                  # 31 --- Omits: i, l, o, 0, 1
        'alpha_numeric_hardtoconfuse_uppercase':                'ABCDEFGHJKLMNPQRSTUVWXYZ23456789',                                 # 32 --- Omits: I, O, 0, 1
    
        'alpha_consonants':                                     'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ',                       # 42 --- Omits: a, e, i, o, u, A, E, I, O, U
        'alpha_consonants_lowercase':                           'bcdfghjklmnpqrstvwxyz',                                            # 21 --- Omits: a, e, i, o, u
        'alpha_consonants_uppercase':                           'BCDFGHJKLMNPQRSTVWXYZ',                                            # 21 --- Omits: A, E, I, O, U
    
        'alpha_numeric_consonants':                             'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ0123456789',             # 52 --- Omits: a, e, i, o, u, A, E, I, O, U
        'alpha_numeric_consonants_lowercase':                   'bcdfghjklmnpqrstvwxyz0123456789',                                  # 31 --- Omits: a, e, i, o, u
        'alpha_numeric_consonants_uppercase':                   'BCDFGHJKLMNPQRSTVWXYZ0123456789',                                  # 31 --- Omits: A, E, I, O, U
    
        'alpha_consonants_hardtoconfuse':                       'bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ',                        # 41 --- Omits: a, e, i, l, o, u, A, E, I, O, U
        'alpha_consonants_hardtoconfuse_lowercase':             'bcdfghjkmnpqrstvwxyz',                                             # 20 --- Omits: a, e, i, l, o, u
        'alpha_consonants_hardtoconfuse_uppercase':             'BCDFGHJKLMNPQRSTVWXYZ',                                            # 21 --- Omits: A, E, I, O, U
    
        'alpha_numeric_consonants_hardtoconfuse':               'bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ23456789',                # 49 --- Omits: a, e, i, l, o, u, A, E, I, O, U, 0, 1
        'alpha_numeric_consonants_hardtoconfuse_lowercase':     'bcdfghjkmnpqrstvwxyz23456789',                                     # 28 --- Omits: a, e, i, l, o, u, 0, 1
        'alpha_numeric_consonants_hardtoconfuse_uppercase':     'BCDFGHJKLMNPQRSTVWXYZ23456789',                                    # 29 --- Omits: A, E, I, O, U, 0, 1
    
        'binary':                                               '01',                                                               # 2  --- Base-2
        'decimal':                                              '0123456789',                                                       # 10 --- Base-10
        'hexadecimal_upper':                                    '0123456789ABCDEF',                                                 # 16 --- Base-16
        'hexadecimal_lower':                                    '0123456789abcdef',                                                 # 16 --- Base-16
        'octal':                                                '01234567',                                                         # 8  --- Base-8
    }

    def random_string(length=32, characters=CHARACTER_SETS['alpha_numeric']):
        random.seed()
        return ''.join(random.choice(characters) for x in range(length))



Similar, in Ruby:

    module Utils
      extend self
      unless defined?(Utils::VALID_CHARS)
        VALID_CHARS = [("a".."z").to_a, ("A".."Z").to_a, ("0".."9").to_a, %w[* $ ! ? ( )]].flatten
        VALID_CHARS_SAFE = VALID_CHARS.clone
        VALID_CHARS_SAFE.delete_if { |v| %w(i l o 1 0).member?(v) }
        VALID_CHARS.freeze
        VALID_CHARS_SAFE.freeze
      end
      def strand(len=12, safe=true)
        chars = safe ? VALID_CHARS_SAFE : VALID_CHARS
        (1..len).collect { chars[rand(chars.size-1)] }.join
      end
    end


you do realize half of this is already included in builtins, right?


No I didn't. Which module? I'll have to RTFM. ;)


import string; string.digits, string.lowercase, string.uppercase, string.hexdigits, string.ascii_


How come your alpha_numerics don't have any numbers in them?


They do at the end?


The scrolling pane fooled me for a second too.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: