From 34901c2a317698b2bec0d2527be66f94130f5609 Mon Sep 17 00:00:00 2001 From: Adolfo Delorenzo Date: Tue, 26 Dec 2023 11:48:13 -0600 Subject: [PATCH] port generator --- gen_ports.py | 39 +++++++++++++++++++++++++++++++++++++++ numbers_db.sqlite | Bin 0 -> 8192 bytes 2 files changed, 39 insertions(+) create mode 100644 gen_ports.py create mode 100644 numbers_db.sqlite diff --git a/gen_ports.py b/gen_ports.py new file mode 100644 index 0000000..d3ec91a --- /dev/null +++ b/gen_ports.py @@ -0,0 +1,39 @@ +import sqlite3 +import random +import uuid + +# Create the SQLite database if it doesn't exist +conn = sqlite3.connect("numbers_db.sqlite") +c = conn.cursor() +c.execute('''CREATE TABLE IF NOT EXISTS numbers (id text, number integer)''') + +def generate_unique_random(): + # Generate a random number between 32768 and 65535 + rand_num = random.randint(32768, 65535) + + # Generate a unique ID using UUID + new_id = str(uuid.uuid4()) + + # Check if the number is already in the database + c.execute("SELECT * FROM numbers WHERE number = ?", (rand_num,)) + results = c.fetchall() + + # If the number doesn't exist in the database, add it to the table + if not results: + c.execute("INSERT INTO numbers VALUES (?, ?)", (new_id, rand_num)) + conn.commit() + else: + # If the number already exists, keep generating new random numbers until a unique one is found + while True: + rand_num = random.randint(32768, 65535) + c.execute("SELECT * FROM numbers WHERE number = ?", (rand_num,)) + results = c.fetchall() + if not results: + new_id = str(uuid.uuid4()) + c.execute("INSERT INTO numbers VALUES (?, ?)", (new_id, rand_num)) + conn.commit() + break + return rand_num + +if __name__ == "__main__": + print("Unique random number: " + str(generate_unique_random())) diff --git a/numbers_db.sqlite b/numbers_db.sqlite new file mode 100644 index 0000000000000000000000000000000000000000..151b57e7d7a70a7ca7cbbb04e52fd8c4a8ba0577 GIT binary patch literal 8192 zcmeI#y=oOf6bJCxdt+`Ivvw9~rV!D^BQvw}*%{@6MG7_X0p?RKBoVGFSokt21fRg( z-ohf}5iD#h6R`0FZfY$p&S8exGyGW&zitj+9;~Ly+uQX?ocPG@u&!fIIA@IQ;GW^W zsSZE3ZW=tV`@hu=d)$8B##GmTVYsMBfCNZ@1W14cNPq-LfCNZ@1W14c{#oF8w|#eS zuUkzq9oN(MC#kOAZI0QqSIhmQB|qAKda&f1Oa5S0_*6ekk2W`abvo5Ib^Y-AZNAw3 z#`^RAbN{h_(cjy>!j_5zNPq-LfCNZ@1W14cNPq-LfCT-43wir6h e23>4Pxqz?QgAS!YswzTrS;o=fAV#q7zy1K-Zgm6z literal 0 HcmV?d00001