Description

This Python script generates lists of unique random (alphanumeric|alpha|numeric) strings from argument defined specifications. Purpose to be used as auxiliary unique random string generation, or stand-alone unique list generation for data population. Each value generated is unique within the outputted list.

Author: Michael Morley (https://michael.morley.cloud)

Source Code

Available on GitHubhttps://github.com/mikmorley/python-unique-random-lists

# Python Random Lists Generator
# -----------------------------
# Description:
#     Generates lists of unique random (alphanumeric|alpha|numeric) strings from argument defined specifications.
#     Purpose to be used as auxilary unique random string generation, or stand alone unique list generation for data population.
#     Each value generated is unique within the outputted list.
# Author: Michael Morley
# Github: https://github.com/mikmorley
# Website: https://michael.morley.cloud

import random
import string
import sys

def argument_help():
    print("")
    print("The following arguments must be defined:")
    print("")
    print("    --count\tTotal count of unique random strings to be generated.")
    print("    --length\tThe length of each unique random string.")
    print("    --uppercase\tDefines if random string should include Uppercase letters. y/n")
    print("    --lowercase\tDefines if random string should include Lowercase letters. y/n")
    print("    --numbers\tDefines if random string should include Numbers. y/n")
    print("")
    print("     python random-lists.py --count <number> --length <number> --uppercase <y/n> --lowercase <y/n> --numbers <y/n>")
    print("")
    print("e.g. python random-lists.py --count 10 --length 24 --uppercase n --lowercase y --numbers y")
    print("")
    sys.exit()

# Checking valid arguments are set.
if len(sys.argv) < 11:
    argument_help()
else:
    try:
        count_idx = sys.argv.index("--count")+1
        length_idx = sys.argv.index("--length")+1
        uppercase_idx = sys.argv.index("--uppercase")+1
        lowercase_idx = sys.argv.index("--lowercase")+1
        numbers_idx = sys.argv.index("--numbers")+1

        arr_length = int(sys.argv[count_idx])
        str_length = int(sys.argv[length_idx])

        if sys.argv[uppercase_idx] == "y":
            has_uppercase = True
        else:
            has_uppercase = False
  
        if sys.argv[lowercase_idx] == "y":
            has_lowercase = True
        else:
            has_lowercase = False

        if sys.argv[numbers_idx] == "y":
            has_numbers = True
        else:
            has_numbers = False

    except:
        argument_help()



# Function to generate a random value based on parameters
def generate_random(str_length, has_uppercase, has_lowercase, has_numbers):
    if has_uppercase and has_lowercase and not has_numbers:
        random_str_req = string.ascii_uppercase + string.ascii_lowercase
    elif has_uppercase and has_numbers and not has_lowercase:
        random_str_req = string.ascii_uppercase + string.digits
    elif has_lowercase and has_numbers and not has_uppercase:
        random_str_req = string.ascii_lowercase + string.digits
    elif has_lowercase and not has_uppercase and not has_numbers:
        random_str_req = string.ascii_lowercase
    elif has_uppercase and not has_lowercase and not has_numbers:
        random_str_req = string.ascii_uppercase
    elif has_numbers and not has_lowercase and not has_uppercase:
        random_str_req = string.digits
    else:
        random_str_req = string.ascii_uppercase + string.ascii_lowercase + string.digits
    x = ''.join(random.choice(random_str_req) for _ in range(str_length))
    return x

# Function to build list of unique randoms based on parameters
def build_list(str_length, has_uppercase, has_lowercase, has_numbers, arr_length):
    i = 0
    unique_values = []
    while i < arr_length:
        
        # Generate initial random value
        value = generate_random(str_length, has_uppercase, has_lowercase, has_numbers)

        # Set iteration constraints
        value_exists = 0
        value_check_count = arr_length
        k = 0

        # Continue iteration until a value that is not present in the list is generated.
        # To prevent infinite iterations, only sub-iterate for the maximum list length.
        while value_exists == 0 and k < value_check_count:
            if value not in unique_values:
                # If value is not present in list, append to list.
                unique_values.append(value)
                value_exists = 1
            else:
                # Value is present in list, a new random value is to be generated, and uniqueness checked again.
                value = generate_random(str_length, has_uppercase, has_lowercase, has_numbers)
                k = k+1

        i = i+1

    return unique_values


# Return the list of unique
list_of_uniques = build_list(str_length, has_uppercase, has_lowercase, has_numbers, arr_length)

# Output List
for value in list_of_uniques:
    print(value)

# Exit after output
sys.exit(0)

Usage

python random-lists.py --count --length --uppercase <y/n> --lowercase <y/n> --numbers <y/n>

The following arguments must be defined:

  • --count Total count of unique random strings to be generated.
  • --length The length of each unique random string.
  • --uppercase Defines if the random string should include Uppercase letters. y/n
  • --lowercase Defines if the random string should include Lowercase letters. y/n
  • --numbers Defines if the random string should include Numbers. y/n

Examples

Generating a unique random list of 10 x 24-bit lowercase alphanumeric strings:

~$ python random-lists.py --count 10 --length 24 --uppercase n --lowercase y --numbers y
uwyrfs1ce1f4iwp5lu96m8s7
h19pmtvoge1266flct1xd5p1
1hjh60equoygr4ph3xqlu4aw
aqrcc4pmppqgowd532582nuu
1s6z3s7dsgof56rykxy0o7sh
02o58m83saq9a2ncvo9ukh0p
uhqtobjndkterrta5h2ruioz
gkztihidjwj8rj4otdp1ziav
jz8vlqbjovukl9qvq7imo01b
k199n3tyaeq3thv5n9t7ap8j

Generating a unique random list of 8 x 6-bit uppercase alphanumeric strings:

~$ python random-lists.py --count 8 --length 6 --uppercase y --lowercase n --numbers y
KXN87F
8KHU6U
SBEAHW
HD35OA
WQ7FW3
GHBXIE
ZGGG9L
BHH2HS

Generating a unique random list of 20 x 32-bit numbers strings:

~$ python random-lists.py --count 20 --length 32 --uppercase n --lowercase n --numbers y
27756443649597453186159388159992
91178778433790435326680545156246
42715778602980314223648153053500
87845631721083594656463793559368
76069833316469702128577438159385
69587429152067598743895495399552
51401615291546516164470336363532
85152468444702315784042549317744
22910112646888912342771840087885
94548958570521095958686600846437
25715442940523329524506424504201
36657180595839112860751751268793
06801304647429105852233372906117
28897690558271126424749705873690
69762251779998735189213061326269
15825308953789844208112991618432
16804890659822884943696325175050
02355145364609947747876222501697
15716518520013563691211846308614
52587265736309625531942623281463

Generating a large unique random list of 10000 x 64-bit alphanumeric strings:

~$ python random-lists.py --count 10000 --length 64 --uppercase y --lowercase y --numbers y
F3wqh1dhCAd3yaSJ3PtHHZTQUUPgRAA5YFe7U4C36uNSPyqlXwdO6S1LbuRJ2cr9
h3wY92PAcO1XwIG7yNrNijmmqUytcfYWfL8cdxBJnunF1a9esfci6xIcfO6E1oDv
JOuazuaIfpHQ04TwZyIHGH2oIs0Dl1o6dGSChMimemfoHq7qxyYGEXAJ3kI4rvXr
AZUqNStHRYq9nL2wm1rLo0gNGtyWYzKcyNFSLrOmHzwzr21oCIjzogOq9i9RIDYG
[...]
mULXcMIBU2H22WDWeqMZPvBo9modUNNLlmN3q2oWLA224RoKQ8LyKyOJgWs2wFOB
jJP4YTUvveRs9gcTSj4mT35NAb8TbQT4pz7dfYyC4Dteiqd6tpyTQCBk5b0ulHJN
yJS8IY9X7PgVgW9kFzUjs1dJYoQB8GQ3Z1rEzbanskETAdMHWWFXzmEZmFxTMXRD
DVhhICjVuTZxaAgYYyeCfNy6lGtbVZK7Lf1j51nnQdfLWq9GZhGajcvsl2cLoKk2
1OMo5cA5iSwwQklFewdE2qcGWDyDx3hTmsTgNQCYTEyW8yzTqUnUAboqtMCGjjPv

Note: This function will only output unique numbers in an array, for example, if you try to generate 200 unique numbers, and you only define a length of 1 bit, you will only be provided with a list of available unique numbers:

~$ python random-lists.py --count 200 --length 1 --uppercase n --lowercase n --numbers y
2
6
0
4
8
5
9
7
1
3