Skip to content

Instantly share code, notes, and snippets.

@Kilo59
Created May 30, 2023 12:37
Show Gist options
  • Save Kilo59/82f227d9dba4e5cce62bc22b245b2638 to your computer and use it in GitHub Desktop.
Save Kilo59/82f227d9dba4e5cce62bc22b245b2638 to your computer and use it in GitHub Desktop.
ID Collisions
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "15a6c959-308a-4cf3-93ed-95ef2411a2f2",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import uuid\n",
"from typing import Callable\n",
"\n",
"def test_collisions(name_gen_func: Callable, number_of_iterations: int) -> int:\n",
" \"\"\"Returns number of collisions given a name generating function and number iterations to perform.\"\"\"\n",
" collisions: int = 0\n",
" unique_names: set[str] = set()\n",
" for i in range(0, number_of_iterations):\n",
" name = name_gen_func()\n",
" if name in unique_names:\n",
" collisions += 1\n",
" else:\n",
" unique_names.add(name)\n",
" return collisions"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "7ba88cab-5a97-470b-9b73-33b8315e3d33",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8608fa32\n",
"0\n",
"4\n",
"113\n"
]
}
],
"source": [
"def short_uuid() -> str:\n",
" return str(uuid.uuid4())[:8]\n",
"\n",
"print(short_uuid())\n",
"print(test_collisions(short_uuid, 10_000))\n",
"print(test_collisions(short_uuid, 100_000))\n",
"print(test_collisions(short_uuid, 1_000_000))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "ffe7a049-dadb-465d-aad4-ae1b97f47ffe",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0a8139bb5d\n",
"0\n",
"0\n",
"1\n",
"11\n"
]
}
],
"source": [
"def medium_uuid() -> str:\n",
" s = str(uuid.uuid4())\n",
" return s.replace(\"-\", \"\")[:10]\n",
"\n",
"print(medium_uuid())\n",
"print(test_collisions(medium_uuid, 10_000))\n",
"print(test_collisions(medium_uuid, 100_000))\n",
"print(test_collisions(medium_uuid, 1_000_000))\n",
"print(test_collisions(medium_uuid, 5_000_000))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment