Nathaniel Knight Consistent, isolated, random UUID generator

Created by Nathaniel Knight last modified Nathaniel Knight

Independent Consistent Pseudo-Random UUID Generators

This module is a demonstration of how to generate sequences of pseudo random UUIDs using Python (and its standard library).

It has a couple of unit tests, which can be run as follows:

python -m unittest test
import random
import uuid

class RandomUuidGenerator:
    """Generates a sequence of pseudo-random UUIDs.

    Given the same seed, it will generate the same sequence.

    def __init__(self, seed):
        self.rng = random.Random(seed)

    def gen_uuid(self):
        return uuid.UUID(
            bytes=bytes(self.rng.getrandbits(8) for _ in range(16)), version=4
import random
import unittest

import generator

SEEDS = ["peanutbutter", "Elrond", "Sonic Screwdriver", 7, (192, 168, 0, 11)]

class TestRepeatedSequences(unittest.TestCase):

    SAMPLE_SIZE = 200

    def test_repeated_sequences(self):
        "Test that the same seed yields the same sequence."
        for seed in SEEDS:
            gen1 = generator.RandomUuidGenerator(seed)
            sample1 = [gen1.gen_uuid() for _ in range(self.SAMPLE_SIZE)]
            gen2 = generator.RandomUuidGenerator(seed)
            sample2 = [gen2.gen_uuid() for _ in range(self.SAMPLE_SIZE)]
                "Expected equal seeds to yield equal sequences",

class TestInterleavedSequences(unittest.TestCase):

    SAMPLE_SIZE = 200

    def test_interleaved_draws_are_independent(self):
        "Check that two generators can co-exist without interferce."
        for seed in SEEDS:
            generators = {
                "left": generator.RandomUuidGenerator(seed),
                "right": generator.RandomUuidGenerator(seed),
            results = {"left": [], "right": []}
            directions = ["left" for _ in range(self.SAMPLE_SIZE)]
            directions.extend("right" for _ in range(self.SAMPLE_SIZE))
            for d in directions:
                gen = generators[d]
                drawn = gen.gen_uuid()
            self.assertEqual(results["left"], results["right"])

Comments (4)

  1. Linda Melson!hfkaj`&encoded=1&redirect=;redirect=[seite]= http://скачивалка.рф/dw/?url=$c&u= http://примгеодезия.рф/?goto= http://мотомагазин.net/forum/away.php?s=|el-3|es-2|cy-1|pt-0&ind1=9897&lang=1&p=pt-43|cy-51|es-44|el-58|ie-60&url=[392]:: Okey Oyna Padlet metin2 pvp serverler Padlet blogamca padlet blogamca padlet blogamca padlet blogamca padlet blogamca padlet metin2 blogamcaa blogamcaa blogamcaa blogamcaa blogamcaa blogamcaa blogamcaa blogamcaa

  2. Linda Melson

    PROFİL BACKLİNK LİST BACKLİNK LİST 2 BACKLİNK LİST 3 zafer2 blog okey zafer2;URL=^E4X&URL=¤t=,TFvar,00319d4f-d81c-4818-81b1-a8413dc614e6,TFvar,GYDH-Y363-YCFJ-DFGH-5R6H,TFvar, http://www.мфц{0}&sno={1}&link=;redirect=

  3. Linda Melson***2&url=[93]+[right_bottom_bottom_1_180]+%D0%BF%D0%B8%D0%B2%D0%BE+%D1%81%D0%BE%D1%87%D0%B8+2017&goto= http://webrad