{"id":2495,"date":"2026-02-02T08:29:45","date_gmt":"2026-02-02T08:29:45","guid":{"rendered":"https:\/\/demo.materiamedica.net\/demo6\/?p=2495"},"modified":"2026-02-02T08:29:45","modified_gmt":"2026-02-02T08:29:45","slug":"numpy-random","status":"publish","type":"post","link":"https:\/\/demo.materiamedica.net\/demo6\/numpy-random\/","title":{"rendered":"NumPy Random"},"content":{"rendered":"<p dir=\"auto\"><strong>NumPy Random<\/strong> \u2014 written as if we are sitting together at a table, I\u2019m showing you examples on my screen, explaining every important concept, showing realistic use cases, warning about common traps, and giving you plenty of runnable code examples.<\/p>\n<p dir=\"auto\">Let\u2019s go step by step \u2014 just like a real class.<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>import numpy as np<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">Why do we need NumPy\u2019s random module?<\/h3>\n<p dir=\"auto\">numpy.random is <strong>the standard way<\/strong> to generate random numbers in almost all scientific, machine learning, data analysis, simulation, and testing work done in Python.<\/p>\n<p dir=\"auto\">Why not use Python\u2019s built-in random module?<\/p>\n<ul dir=\"auto\">\n<li>np.random is <strong>much faster<\/strong> for generating large arrays<\/li>\n<li>It produces <strong>ndarrays<\/strong> directly (ready for math, slicing, reshaping\u2026)<\/li>\n<li>It has <strong>far more distributions<\/strong> (normal, uniform, binomial, poisson, beta, gamma\u2026)<\/li>\n<li>It supports <strong>seeding<\/strong> for perfect reproducibility<\/li>\n<li>It works beautifully with vectorization and broadcasting<\/li>\n<\/ul>\n<p dir=\"auto\"><strong>Golden rule #1 (write this down):<\/strong><\/p>\n<blockquote dir=\"auto\">\n<p dir=\"auto\">Whenever you need random numbers for data science, ML, simulations, or testing \u2192 use numpy.random \u2014 not random.<\/p>\n<\/blockquote>\n<h3 dir=\"auto\">Step 1 \u2013 The most important habit: Setting the random seed<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>np.random.seed(42)          # \u2190 this line makes all random results reproducible<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Every time you run your code with the same seed \u2192 you get <strong>exactly the same random numbers<\/strong>.<\/p>\n<p dir=\"auto\">This is <strong>extremely important<\/strong> when:<\/p>\n<ul dir=\"auto\">\n<li>Debugging<\/li>\n<li>Comparing experiments<\/li>\n<li>Sharing code<\/li>\n<li>Writing tests<\/li>\n<li>Teaching \/ tutorials<\/li>\n<\/ul>\n<p dir=\"auto\"><strong>Example \u2013 without seed vs with seed<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code># Without seed \u2192 different every time\r\nprint(np.random.rand(5))\r\n\r\n# With seed \u2192 always the same\r\nnp.random.seed(42)\r\nprint(np.random.rand(5))\r\n# [0.37454012 0.95071431 0.73199394 0.59865848 0.15601864]<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Teacher tip<\/strong>: Put np.random.seed(42) (or any fixed number) at the top of your notebooks\/scripts when you start learning or experimenting. Later, remove it (or change it) when you want truly random behavior.<\/p>\n<h3 dir=\"auto\">Step 2 \u2013 Most commonly used random functions<\/h3>\n<h4 dir=\"auto\">1. np.random.rand() \u2013 Uniform random numbers in [0, 1)<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code># Single value\r\nprint(np.random.rand())             # e.g. 0.3745401188473625\r\n\r\n# 1D array\r\nprint(np.random.rand(8))\r\n\r\n# 2D array (very common)\r\nprint(np.random.rand(4, 5))\r\n\r\n# 3D array (e.g. batch of images)\r\nprint(np.random.rand(32, 64, 64, 3).shape)   # (32, 64, 64, 3)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Realistic use case<\/strong> \u2013 creating synthetic features<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code># 1000 samples, 20 features\r\nX_synthetic = np.random.rand(1000, 20)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">2. np.random.randn() \u2013 Standard normal (Gaussian) distribution<\/h4>\n<p dir=\"auto\">Mean = 0, standard deviation = 1<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>print(np.random.randn(10))\r\n# example: [-0.49671415  0.7680537   0.0884925  ...]\r\n\r\n# Very common shape in ML\r\nweights = np.random.randn(784, 10)   # weights matrix for neural network layer<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Quick comparison<\/strong> \u2013 rand vs randn<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>uniform = np.random.rand(10000)\r\nnormal  = np.random.randn(10000)\r\n\r\n# uniform \u2192 flat between 0 and 1\r\n# normal  \u2192 bell curve around 0<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">3. np.random.randint() \u2013 Random integers<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code># Single integer between low (inclusive) and high (exclusive)\r\nprint(np.random.randint(1, 7))           # dice roll: 1\u20136\r\n\r\n# Array of dice rolls\r\ndice = np.random.randint(1, 7, size=20)\r\nprint(dice)\r\n\r\n# 2D \u2013 like scores or pixel values\r\nscores = np.random.randint(40, 101, size=(50, 4))   # 50 students \u00d7 4 subjects<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">4. np.random.uniform() \u2013 Uniform with custom range<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code># Uniform between -5 and 5\r\nprint(np.random.uniform(-5, 5, size=10))\r\n\r\n# Temperature simulation (18\u201332 \u00b0C)\r\ntemps = np.random.uniform(18, 32, size=365)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">5. np.random.normal() \u2013 Normal with custom mean &amp; std<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code># IQ scores: mean=100, std=15\r\niq_scores = np.random.normal(100, 15, size=1000)\r\n\r\n# Noisy measurements\r\ntrue_value = 50.0\r\nnoisy = true_value + np.random.normal(0, 0.8, size=200)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">6. np.random.choice() \u2013 Sampling from existing array<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>fruits = np.array(['apple', 'banana', 'orange', 'mango', 'grapes'])\r\n\r\n# Single choice\r\nprint(np.random.choice(fruits))\r\n\r\n# 20 choices with replacement\r\nprint(np.random.choice(fruits, size=20))\r\n\r\n# Without replacement (unique)\r\nprint(np.random.choice(fruits, size=3, replace=False))\r\n\r\n# With probabilities\r\nprobs = [0.5, 0.2, 0.1, 0.1, 0.1]\r\nbiased = np.random.choice(fruits, size=100, p=probs)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">7. np.random.shuffle() \u2013 Shuffle in place<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>deck = np.arange(1, 53)   # cards 1 to 52\r\nnp.random.shuffle(deck)\r\nprint(deck[:10])          # first 10 cards after shuffle<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Note<\/strong>: shuffle modifies the array in place \u2014 no return value.<\/p>\n<h3 dir=\"auto\">Step 3 \u2013 Realistic &amp; common use cases (you will write these often)<\/h3>\n<p dir=\"auto\"><strong>Use case 1 \u2013 Creating synthetic training data<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>np.random.seed(42)\r\n\r\nn_samples = 1000\r\nX = np.random.randn(n_samples, 5) * 2 + 10          # centered around 10\r\nnoise = np.random.randn(n_samples) * 0.5\r\ny = 3 * X[:,0] + 1.5 * X[:,1] - 2 * X[:,2] + noise  # linear relation + noise<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Use case 2 \u2013 Train \/ validation \/ test split (manual)<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>data = np.random.randn(10000, 30)\r\nnp.random.shuffle(data)                     # important!\r\n\r\ntrain = data[:7000]\r\nval   = data[7000:8500]\r\ntest  = data[8500:]<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Use case 3 \u2013 Random missing values simulation<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>measurements = np.random.uniform(20, 80, 500)\r\nmask = np.random.random(500) &lt; 0.1          # 10% missing\r\nmeasurements[mask] = np.nan<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Use case 4 \u2013 Random image-like noise<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>img = np.ones((200, 300, 3)) * 128          # gray image\r\nnoise = np.random.normal(0, 25, img.shape)\r\nnoisy_img = np.clip(img + noise, 0, 255).astype(np.uint8)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">Summary \u2013 Quick Reference Table<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"md\">Function<\/th>\n<th data-col-size=\"xl\">Distribution \/ Behavior<\/th>\n<th data-col-size=\"lg\">Typical shape example<\/th>\n<th data-col-size=\"xs\">Common seed usage<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"md\">rand()<\/td>\n<td data-col-size=\"xl\">Uniform [0, 1)<\/td>\n<td data-col-size=\"lg\">rand(1000, 20)<\/td>\n<td data-col-size=\"xs\">Yes<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">randn()<\/td>\n<td data-col-size=\"xl\">Standard normal (\u03bc=0, \u03c3=1)<\/td>\n<td data-col-size=\"lg\">randn(784, 10)<\/td>\n<td data-col-size=\"xs\">Yes<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">randint(low, high)<\/td>\n<td data-col-size=\"xl\">Integers [low, high)<\/td>\n<td data-col-size=\"lg\">randint(1, 7, size=100)<\/td>\n<td data-col-size=\"xs\">Yes<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">uniform(low, high)<\/td>\n<td data-col-size=\"xl\">Uniform [low, high)<\/td>\n<td data-col-size=\"lg\">uniform(18, 32, 365)<\/td>\n<td data-col-size=\"xs\">Yes<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">normal(loc, scale)<\/td>\n<td data-col-size=\"xl\">Normal \u03bc=loc, \u03c3=scale<\/td>\n<td data-col-size=\"lg\">normal(100, 15, 1000)<\/td>\n<td data-col-size=\"xs\">Yes<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">choice()<\/td>\n<td data-col-size=\"xl\">Sample from given array<\/td>\n<td data-col-size=\"lg\">choice(fruits, 50)<\/td>\n<td data-col-size=\"xs\">Yes<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">shuffle()<\/td>\n<td data-col-size=\"xl\">Shuffle array in place<\/td>\n<td data-col-size=\"lg\">shuffle(deck)<\/td>\n<td data-col-size=\"xs\">Yes<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">Final teacher advice<\/h3>\n<p dir=\"auto\"><strong>Always<\/strong> start your notebooks\/experiments with:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>import numpy as np\r\nnp.random.seed(42)     # or any fixed number you like<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">This small habit will save you many hours of confusion later.<\/p>\n<p dir=\"auto\">Where would you like to go next?<\/p>\n<ul dir=\"auto\">\n<li>Random distributions in more depth (binomial, poisson, beta\u2026)<\/li>\n<li>Randomness in machine learning (data splitting, weight init, dropout\u2026)<\/li>\n<li>Common bugs when using random numbers<\/li>\n<li>Mini-project: simulate data + add noise + clean it<\/li>\n<li>Reproducibility across multiple runs \/ files<\/li>\n<\/ul>\n<p dir=\"auto\">Just tell me what you want to focus on now! \ud83d\ude0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>NumPy Random \u2014 written as if we are sitting together at a table, I\u2019m showing you examples on my screen, explaining every important concept, showing realistic use cases, warning about common traps, and giving&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[75],"tags":[],"class_list":["post-2495","post","type-post","status-publish","format-standard","hentry","category-numpy"],"_links":{"self":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2495","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/comments?post=2495"}],"version-history":[{"count":1,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2495\/revisions"}],"predecessor-version":[{"id":2496,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2495\/revisions\/2496"}],"wp:attachment":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/media?parent=2495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/categories?post=2495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/tags?post=2495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}