A Random Generator for Multiple Scenarios
import base64
import hashlib
import hmac
import random
import secrets
import uuid


def gen_rawdata() -> str:
    rawdata_1 = secrets.token_urlsafe(None)
    random.seed(str(uuid.uuid4()))
    key = random.randint(1, 32)
    rawdata_2 = str(random.randbytes(key))
    rawdata_raw = rawdata_1 + rawdata_2
    mykdf = hashlib.pbkdf2_hmac(
        "sha256", rawdata_raw.encode("utf-8"), b"", 10**6, dklen=16
    )
    the_uuid = str(uuid.UUID(bytes=mykdf)).upper()
    rawdata = hmac.new(
        bytes(rawdata_2.encode()), the_uuid.encode(), hashlib.sha256
    ).hexdigest()
    return rawdata


def gen_random_int(a: int, b: int) -> int:
    random.seed(gen_rawdata())
    return random.randint(a, b)


DEFALT_NUMBER_OF_BYTES = 32


def gen_random_bytes(nbytes: int = None) -> bytes:
    if nbytes is None:
        nbytes = DEFALT_NUMBER_OF_BYTES
    random.seed(gen_rawdata())
    return random.randbytes(nbytes)


def gen_random_urlsafe_token(nbytes: int = None) -> str:
    tok = gen_random_bytes(nbytes)
    return base64.urlsafe_b64encode(tok).rstrip(b"=").decode("ascii")


def gen_random_single_digit() -> int:
    string = gen_rawdata()
    h = hashlib.sha3_512()
    h.update(string.encode("utf-8"))
    n = int(h.hexdigest(), base=16)
    s = 0
    while True:
        s += n % 10
        n //= 10
        if n > 10:
            break
        else:
            continue
    return s


def gen_random_true_or_false() -> bool:
    num = gen_random_single_digit()
    if num % 2 == 0:
        return True
    else:
        return False

src/randgen/__init__.py
hrhszsdtc/py-random-generator
from __init__ import *

print(gen_rawdata(), type(gen_rawdata()))
print(gen_random_int(1, 32), type(gen_random_int(1, 32)))
print(gen_random_bytes(None), type(gen_random_bytes(None)))
print(gen_random_urlsafe_token(None), type(gen_random_urlsafe_token(None)))
print(gen_random_single_digit(), type(gen_random_single_digit()))
print(gen_random_true_or_false(), type(gen_random_true_or_false()))

src/randgen/example.py
hrhszsdtc/py-random-generator
import base64
import hashlib
import hmac
import random
import secrets
import unittest
import uuid

from randgen import (
    gen_random_bytes,
    gen_random_int,
    gen_random_single_digit,
    gen_random_true_or_false,
    gen_random_urlsafe_token,
    gen_rawdata,
)


class TestCode(unittest.TestCase):
    def test_gen_rawdata(self):
        rawdata = gen_rawdata()
        self.assertIsInstance(rawdata, str)
        self.assertEqual(len(rawdata), 64)
        self.assertTrue(all(c in "0123456789abcdef" for c in rawdata))

    def test_gen_random_int(self):
        a = 10
        b = 20
        random_int = gen_random_int(a, b)
        self.assertIsInstance(random_int, int)
        self.assertTrue(a <= random_int <= b)

    def test_gen_random_bytes(self):
        nbytes = 16
        random_bytes = gen_random_bytes(nbytes)
        self.assertIsInstance(random_bytes, bytes)
        self.assertEqual(len(random_bytes), nbytes)

    def test_gen_random_urlsafe_token(self):
        nbytes = 16
        random_token = gen_random_urlsafe_token(nbytes)
        self.assertIsInstance(random_token, str)
        self.assertEqual(len(random_token), (nbytes * 4 + 2) // 3)
        self.assertTrue(
            all(
                c in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
                for c in random_token
            )
        )

    def test_gen_random_single_digit(self):
        random_digit = gen_random_single_digit()
        self.assertIsInstance(random_digit, int)
        self.assertTrue(0 <= random_digit <= 9)

    def test_gen_random_true_or_false(self):
        random_bool = gen_random_true_or_false()
        self.assertIsInstance(random_bool, bool)


if __name__ == "__main__":
    unittest.main()

tests/__init__.py
hrhszsdtc/py-random-generator