Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Create an x amount of unique random fixed size strings

#!/usr/bin/env python

from typing import Generator
from random import SystemRandom as RND
from string import ascii_uppercase, digits


def string_generator(size: int = 1, amount: int = 1) -> Generator[str, None, None]:
    """
    Return x random strings of a fixed length.

    :param size: string length, defaults to 1
    :type size: int, optional
    :param amount: amount of random strings to generate, defaults to 1
    :type amount: int, optional
    :yield: Yield composed random string if unique
    :rtype: Generator[str, None, None]
    """
    CHARS = list(ascii_uppercase + digits)
    LIMIT = len(CHARS) ** size
    count, check, string = 0, set(), ''
    while LIMIT > count < amount:
        string = ''.join(RND().choices(CHARS, k=size))
        if string not in check:
            check.add(string)
            yield string
            count += 1


for my_count, my_string in enumerate(string_generator(6, 20)):
    print(my_count, my_string)
Comment

PREVIOUS NEXT
Code Example
Python :: how to use the "import random" in-built model in python 
Python :: convert to category data type 
Python :: How to make colors.winapp in WindowsAP 
Python :: axes in array 
Python :: Extracting the cluster labels from a dendrogram 
Python :: django chain query 
Python :: flask how to initialze extension after start 
Python :: fecthone 
Python :: HIDING AND ENCRYPTING PASSWORDS IN PYTHON USING ADVPASS 
Python :: how to print continuesly in the same line in python 
Python :: how to prefix numbers with zero in python 
Python :: Como hacer mayusculas un string 
Python :: buscar elemento en lista python 
Python :: how to install opencv for python 3.7.3 
Python :: 1045 - Triangle Types 
Python :: python matrices access column 
Python :: how to increase width of line in graph of linear regression in matplotlib 
Python :: pandas fast way to view distribution by group 
Python :: quoto x discord selfbot 
Python :: pyqgis 
Python :: re.add python 
Python :: make_interp_spline 
Python :: Console code page (437) differs from Windows code page (1252) 8-bit characters might not work correctly 
Python :: how to access a variable from another py file in vs code 
Python :: pylatex add section without numbering 
Python :: dorp ligne in df where values equal zeros 
Python :: Merge the values for each key using an associative and commutative reduce function. 
Python :: calling a function in python upon entry content changing tkinter 
Python :: timedelta64 total_mins 
Python :: set list start at 1 python 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =