import sqlite3 import uuid import math import time DB = "uuid_test.db" # SQLite-Datenbank öffnen/erstellen conn = sqlite3.connect(DB) cursor = conn.cursor() cursor.execute(""" CREATE TABLE IF NOT EXISTS uuids ( uuid TEXT PRIMARY KEY ) """) conn.commit() n = 0 start = time.time() # UUIDv4 hat 122 zufällige Bits N = 2**122 print("UUIDv4-Kollisionstest gestartet!") print("Strg+C zum Beenden.\n") try: while True: new_uuid = str(uuid.uuid4()) n += 1 # Einfügen versuchen try: cursor.execute( "INSERT INTO uuids (uuid) VALUES (?)", (new_uuid,) ) conn.commit() duplicate = False except sqlite3.IntegrityError: # UUID existiert bereits duplicate = True # Kollisionswahrscheinlichkeit probability = 1 - math.exp( -(n * (n - 1)) / (2 * N) ) print( f"\rUUIDs: {n:,} | " f"Letzte: {new_uuid} | " f"Doppelt: {'JA' if duplicate else 'NEIN'} | " f"Kolliw.: {probability:.12e}", end="", flush=True ) if duplicate: print("\n\n!!! KOLLISION GEFUNDEN !!!") print(f"UUID: {new_uuid}") print(f"Nach {n:,} Versuchen") break except KeyboardInterrupt: print("\n\nTest beendet.") finally: conn.close()