Skip to content

Instantly share code, notes, and snippets.

@banan314
Created June 5, 2024 16:59
Show Gist options
  • Save banan314/5bbff826f4408cd45f8a0a579380d1af to your computer and use it in GitHub Desktop.
Save banan314/5bbff826f4408cd45f8a0a579380d1af to your computer and use it in GitHub Desktop.
Random generator tools for learning 🇪🇸
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"def remove_duplication(arr):\n",
" if not arr:\n",
" return []\n",
" \n",
" result = [arr[0]]\n",
" for i in range(1, len(arr)):\n",
" if arr[i] != arr[i - 1]:\n",
" result.append(arr[i])\n",
" \n",
" return result\n",
"\n",
"# Function to normalize the probabilities\n",
"def normalize_weights(weights):\n",
" total = sum(weights.values())\n",
" return {letter: weight / total for letter, weight in weights.items()}"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[18, 39, 79, 27, 64, 21, 66, 85, 65, 63, 87, 70, 85, 26, 62, 27, 92, 85, 73, 53, 69, 45, 92, 93, 88]\n",
"[78, 67, 79, 16, 78, 64, 36, 90, 79, 89, 73, 39, 28, 52, 74, 70, 10, 58, 62, 57, 69, 72, 66, 49, 80]\n",
"[20, 56, 74, 37, 68, 72, 83, 68, 71, 72, 81, 25, 71, 53, 84, 33, 43, 23, 71, 75, 72, 67, 18, 75, 46]\n",
"[39, 75, 23, 79, 56, 69, 28, 83, 99, 53, 72, 20, 77, 72, 47, 37, 57, 55, 28, 34, 54, 13]\n"
]
}
],
"source": [
"import random\n",
"\n",
"number_weights = { n: 1 for n in range(10, 100) }\n",
"for i in range(60, 80):\n",
" number_weights[i] = 3\n",
"\n",
"for i in range(15, 20):\n",
" number_weights[i] = 3\n",
"\n",
"# Generate 100 random numbers between 10 and 100\n",
"random_numbers = random.choices(\n",
" population=list(number_weights.keys()), \n",
" weights=list(number_weights.values()),\n",
" k=100\n",
")\n",
"random_numbers = remove_duplication(random_numbers)\n",
"\n",
"# Print the random numbers\n",
"for i in range(0, 100, 25):\n",
" print(random_numbers[i:i+25])\n"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 8:23 12:08 14:01 0:41 11:24 16:34 19:27 18:28 8:39 14:42 5:52 21:24 9:55 13:40 1:42\n",
"13:07 21:58 5:36 1:24 14:59 21:02 16:13 13:45 17:24 22:51 14:06 23:49 22:54 4:32 6:47\n",
"16:24 1:50 23:19 20:33 10:41 9:46 22:31 5:39 8:17 12:03 3:56 16:19 6:20 23:28 9:43\n",
" 1:00 13:21 13:15 0:14 13:09 18:24 19:08 3:38 22:34 2:07 15:47 16:25 12:34 22:56 17:28\n",
"18:57 9:29 6:32 4:05 10:34 14:03 22:30 7:56 5:26 10:38 6:41 6:39 11:35 20:02 6:29\n",
" 4:12 6:38 22:10 19:11 20:39 23:14 2:57 21:26 19:55 1:17 14:58 4:04 10:37 14:35 15:05\n",
"16:59 12:22 9:20 13:06 3:44 11:14 16:00 2:11 18:51\n"
]
}
],
"source": [
"# Generate 100 random hours between 0 and 23\n",
"random_times = [f\"{random.randint(0, 23):2d}:{random.randint(0, 59):02d}\" for _ in range(100)]\n",
"random_times = remove_duplication(random_times)\n",
"\n",
"# Print the random times with n times per line\n",
"n = 15\n",
"for i in range(0, 100, n):\n",
" print(\" \".join(random_times[i:i+n]))"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 57,50 € 83,70 € 73,68 € 54,77 € 64,73 € 65,63 € 65,21 € 38,09 € 83,23 € 65,68 € 73,05 € 32,45 € 49,68 € 60,91 € 65,32 €\n",
" 17,62 € 74,41 € 89,38 € 75,12 € 70,38 € 73,85 € 78,79 € 30,18 € 69,41 € 59,80 € 72,64 € 27,15 € 90,66 € 80,55 € 38,98 €\n",
" 77,72 € 63,75 € 60,77 € 84,76 € 13,26 € 77,62 € 77,54 € 36,16 € 30,74 € 17,51 € 61,66 € 25,07 € 65,67 € 16,26 € 73,18 €\n",
" 77,67 € 20,28 € 76,25 € 80,25 € 15,71 € 52,56 € 66,48 € 16,32 € 65,94 € 11,68 € 16,05 € 13,09 € 92,28 € 91,67 € 77,15 €\n",
" 71,41 € 44,97 € 57,63 € 91,72 € 74,60 € 70,30 € 62,44 € 54,62 € 64,95 € 75,53 € 58,50 € 96,57 € 16,60 € 90,84 € 46,64 €\n",
" 19,25 € 88,78 € 33,97 € 62,09 € 15,11 € 72,02 € 90,72 € 18,88 € 64,25 € 56,63 € 78,61 € 35,63 € 23,79 € 70,62 € 48,74 €\n",
" 97,34 € 28,75 € 65,82 € 51,65 € 17,20 € 68,71 € 17,01 € 71,47 € 92,67 € 72,59 €\n"
]
}
],
"source": [
"euro_weights = { n: 1 for n in range(10, 100) }\n",
"for i in range(60, 80):\n",
" euro_weights[i] = 3\n",
"\n",
"for i in range(15, 20):\n",
" euro_weights[i] = 3\n",
"\n",
"cent_weights = { n: 1 for n in range(0, 99) }\n",
"for i in range(60, 80):\n",
" cent_weights[i] = 3\n",
"\n",
"cent_weights[100] = 3\n",
"\n",
"# Normalize the initial weights\n",
"euro_normalized_weights = normalize_weights(euro_weights)\n",
"cent_normalized_weights = normalize_weights(cent_weights)\n",
"\n",
"def random_euro():\n",
" return random.choices(\n",
" population=list(euro_normalized_weights.keys()), \n",
" weights=list(euro_normalized_weights.values()), \n",
" k=1\n",
" )[0]\n",
"\n",
"def random_cent():\n",
" return random.choices(\n",
" population=list(cent_normalized_weights.keys()), \n",
" weights=list(cent_normalized_weights.values()), \n",
" k=1\n",
" )[0]\n",
"\n",
"# Generate 100 random prices between 10 and 100\n",
"random_prices = [f\"{random_euro():3d},{random_cent():02d} €\" for _ in range(100)]\n",
"\n",
"# Print the random times with n times per line\n",
"n = 15\n",
"for i in range(0, 100, n):\n",
" print(\" \".join(random_prices[i:i+n]))"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"r, l, ñ, d, r, q, g, j, h, t, n, j, d, j, g, a, v, c, ñ, b, i, u, g, u, m\n",
"e, t, o, l, g, x, a, u, j, i, e, v, j, d, i, d, s, l, v, q, n, f, a, q, n\n",
"j, x, h, j, o, v, d, v, l, b, l, f, p, q, h, n, g, v, a, y, t, w, v, k, u\n",
"y, n, q, a, y, o, g, b, s, t, j, p, z, v, b, r, l, n, m, i\n"
]
}
],
"source": [
"import string\n",
"\n",
"# Define the letters including ñ\n",
"letters = list(string.ascii_lowercase) + ['ñ']\n",
"\n",
"# Initialize the probabilities map with ones\n",
"letter_weights = {letter: 1 for letter in letters}\n",
"letter_weights['j'] = 3\n",
"\n",
"# Normalize the initial weights\n",
"normalized_weights = normalize_weights(letter_weights)\n",
"\n",
"# Generate 100 random letters based on the normalized probabilities\n",
"random_letters = random.choices(\n",
" population=list(normalized_weights.keys()), \n",
" weights=list(normalized_weights.values()), \n",
" k=100\n",
")\n",
"\n",
"random_letters = remove_duplication(random_letters)\n",
"\n",
"# Print the random letters with 25 letters per line\n",
"for i in range(0, 100, 25):\n",
" print(\", \".join(random_letters[i:i+25]))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment