Created
February 28, 2022 17:29
-
-
Save waterrmalann/172959516c06f3da6d0fe6b556dae0a8 to your computer and use it in GitHub Desktop.
A python module that can generate random numbers between two integer ranges that are easy to remember and read.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Memorable Random Int (originally Memorable One-Time Password Generator) | |
# Written by Alan Varghese (waterrmalann) | |
""" | |
A random number generator (between two ranges) that outputs a number that is easy to read and remember. | |
Can be used to generate OTPs that stay in short term memory but may not be that secure due to the limited set of numbers. | |
""" | |
from random import choice | |
import string | |
class MOTPGenerator: | |
def __init__(self, minimum: int, maximum: int, prepend_alpha: bool = False): | |
self.minimum = minimum | |
self.maximum = maximum | |
self.prepend_alpha = prepend_alpha | |
self.number_list = self.generate_list(minimum, maximum) | |
def __call__(self): | |
if self.prepend_alpha: | |
return f"{choice(string.ascii_uppercase)}-{choice(self.number_list)}" | |
else: | |
return choice(self.number_list) | |
# 1020 | |
# 1010 | |
# 40 | |
def generate_list(self, minimum: int, maximum: int) -> list: | |
num_list = [] | |
for num in range(minimum, maximum): | |
num_as_str = str(num) | |
if self.is_palindrome(num_as_str): | |
# palindrome | |
num_list.append(num) | |
elif self.is_repeating(num_as_str): | |
# repeating | |
num_list.append(num) | |
elif not (num % 100): | |
# divisible by 100 | |
num_list.append(num) | |
elif len(set(num_as_str)) <= 2: | |
# common | |
num_list.append(num) | |
elif num_as_str in string.digits: | |
# increasing | |
num_list.append(num) | |
elif num_as_str in string.digits[::-1]: | |
# decreasing | |
num_list.append(num) | |
else: | |
continue | |
return num_list | |
def is_palindrome(self, s: str) -> bool: | |
return s[::-1] == s | |
def is_repeating(self, s: str) -> bool: | |
return (s + s).find(s, 1, -1) != -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment