{"id":2497,"date":"2026-02-02T08:33:14","date_gmt":"2026-02-02T08:33:14","guid":{"rendered":"https:\/\/demo.materiamedica.net\/demo6\/?p=2497"},"modified":"2026-02-02T08:33:14","modified_gmt":"2026-02-02T08:33:14","slug":"chapter-1-random-numbers-in-numpy","status":"publish","type":"post","link":"https:\/\/demo.materiamedica.net\/demo6\/chapter-1-random-numbers-in-numpy\/","title":{"rendered":"Chapter 1: Random Numbers in NumPy"},"content":{"rendered":"<p dir=\"auto\"><strong>Random Numbers in NumPy<\/strong> \u2014 written as if I\u2019m your patient teacher sitting next to you, showing examples on the screen, explaining every important detail, giving realistic use-cases, warning about common mistakes, and helping you build good habits from the beginning.<\/p>\n<p dir=\"auto\">Let\u2019s imagine we\u2019re working together in a Jupyter notebook. Ready? \ud83d\ude0a<\/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\">1. Why NumPy random instead of Python\u2019s random?<\/h3>\n<p dir=\"auto\">Many beginners start with Python\u2019s built-in random module.<\/p>\n<p dir=\"auto\">But in serious numerical\/scientific\/ML work you should <strong>almost always<\/strong> use numpy.random.<\/p>\n<p dir=\"auto\">Main reasons:<\/p>\n<ul dir=\"auto\">\n<li>Much <strong>faster<\/strong> when generating thousands\/millions of numbers<\/li>\n<li>Returns <strong>NumPy arrays<\/strong> directly (ready for math, slicing, broadcasting)<\/li>\n<li>Much <strong>larger variety<\/strong> of distributions (normal, binomial, poisson, beta, gamma, multivariate\u2026)<\/li>\n<li>Proper <strong>multi-dimensional support<\/strong> out of the box<\/li>\n<li>Consistent <strong>seeding<\/strong> behavior across different functions<\/li>\n<li>Better integration with the rest of the NumPy ecosystem<\/li>\n<\/ul>\n<p dir=\"auto\"><strong>Golden rule #1 (write it somewhere):<\/strong><\/p>\n<blockquote dir=\"auto\">\n<p dir=\"auto\">If you&#8217;re doing anything numerical\/scientific\/ML\/data analysis \u2192 <strong>use numpy.random<\/strong> \u2014 not random, not secrets, not anything else.<\/p>\n<\/blockquote>\n<h3 dir=\"auto\">2. The MOST important habit: Always control the 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 single line changes everything<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Why is this so important?<\/strong><\/p>\n<ul dir=\"auto\">\n<li>Makes experiments <strong>reproducible<\/strong><\/li>\n<li>Makes debugging <strong>much easier<\/strong><\/li>\n<li>Allows others to get <strong>exactly the same results<\/strong><\/li>\n<li>Extremely helpful when teaching, writing tutorials, comparing models<\/li>\n<\/ul>\n<p dir=\"auto\"><strong>Quick demonstration:<\/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 you run\r\nprint(np.random.rand(5))\r\n\r\n# With seed \u2192 always exactly the same numbers\r\nnp.random.seed(42)\r\nprint(np.random.rand(5))\r\n# \u2192 [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>: During learning\/experimenting \u2192 <strong>always<\/strong> put a fixed seed at the top of your notebook\/script. When you go to production or want true randomness \u2192 remove\/comment out the seed line.<\/p>\n<h3 dir=\"auto\">3. The most frequently used random functions (you&#8217;ll use these 90% of the time)<\/h3>\n<h4 dir=\"auto\">A. np.random.rand() \u2014 Uniform random numbers [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 number\r\nprint(np.random.rand())\r\n\r\n# 1D vector\r\nprint(np.random.rand(10))\r\n\r\n# 2D matrix (very common!)\r\nprint(np.random.rand(4, 6))\r\n\r\n# 4D \u2014 batch of images (common in deep learning)\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<h4 dir=\"auto\">B. np.random.randn() \u2014 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(12))\r\n# typical output: mixture of positive &amp; negative, most values between -3 and +3\r\n\r\n# Very common shape in neural networks\r\nweights_layer1 = np.random.randn(784, 128)   # 784 input \u2192 128 neurons<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Quick comparison table students should remember:<\/strong><\/p>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"sm\">Function<\/th>\n<th data-col-size=\"lg\">Range \/ Distribution<\/th>\n<th data-col-size=\"lg\">Typical shape example<\/th>\n<th data-col-size=\"xl\">Most common use case<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"sm\">rand()<\/td>\n<td data-col-size=\"lg\">[0, 1) uniform<\/td>\n<td data-col-size=\"lg\">rand(1000, 30)<\/td>\n<td data-col-size=\"xl\">Features, dropout masks, probabilities<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"sm\">randn()<\/td>\n<td data-col-size=\"lg\">~ Normal(0, 1)<\/td>\n<td data-col-size=\"lg\">randn(512, 512, 3)<\/td>\n<td data-col-size=\"xl\">Weights init, noise, synthetic data<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"sm\">randint()<\/td>\n<td data-col-size=\"lg\">integers [low, high)<\/td>\n<td data-col-size=\"lg\">randint(1, 7, size=100)<\/td>\n<td data-col-size=\"xl\">Dice, labels, indices, pixel values<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">C. np.random.randint(low, high, size=&#8230;) \u2014 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 dice roll (1\u20136)\r\nprint(np.random.randint(1, 7))\r\n\r\n# 1000 coin flips (0 or 1)\r\ncoins = np.random.randint(0, 2, size=1000)\r\n\r\n# Realistic: exam scores (0\u2013100)\r\nscores = np.random.randint(35, 101, size=(200, 5))  # 200 students \u00d7 5 subjects<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">D. np.random.uniform(low, high, size=&#8230;) \u2014 Uniform in 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># Temperatures between 18\u201332 \u00b0C\r\ntemps = np.random.uniform(18, 32, size=365)\r\n\r\n# Prices between 9.99 and 99.99\r\nprices = np.random.uniform(9.99, 99.99, size=500)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">E. np.random.normal(loc, scale, size=&#8230;) \u2014 Custom normal distribution<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code># Realistic IQ scores\r\niq = np.random.normal(loc=100, scale=15, size=10000)\r\n\r\n# Measurement with sensor noise (\u00b12 units)\r\ntrue_distance = 150.0\r\nmeasured = true_distance + np.random.normal(0, 2, size=200)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">4. Very common realistic use cases (you will write these many times)<\/h3>\n<p dir=\"auto\"><strong>Use case 1 \u2013 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 = 5000\r\nX = np.random.randn(n, 8) * 3 + 50               # centered around 50\r\nnoise = np.random.normal(0, 4, n)\r\n\r\ny = (2.5 * X[:,0] - 1.8 * X[:,2] + 0.9 * X[:,5]) + noise<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Use case 2 \u2013 Adding realistic noise to images<\/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.full((300, 400, 3), 180, dtype=np.uint8)  # light gray\r\nnoise = np.random.normal(0, 30, img.shape)\r\nnoisy = np.clip(img + noise, 0, 255).astype(np.uint8)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Use case 3 \u2013 Random train\/val\/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(12000, 25)\r\n\r\nnp.random.shuffle(data)          # \u2190 very important step!\r\n\r\ntrain = data[:9000]\r\nval   = data[9000:10500]\r\ntest  = data[10500:]<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Use case 4 \u2013 Random sampling \/ bootstrapping<\/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>population = np.random.normal(100, 20, 100000)\r\n\r\n# Bootstrap sample of 1000\r\nsample = np.random.choice(population, size=1000, replace=True)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">Summary \u2013 Your 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\">What you get<\/th>\n<th data-col-size=\"lg\">Typical shape<\/th>\n<th data-col-size=\"xs\">Typical 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(512,512,3)<\/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,101,10000)<\/td>\n<td data-col-size=\"xs\">Yes<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">uniform(a,b)<\/td>\n<td data-col-size=\"xl\">Uniform [a,b)<\/td>\n<td data-col-size=\"lg\">uniform(0,100,500)<\/td>\n<td data-col-size=\"xs\">Yes<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">normal(\u03bc,\u03c3)<\/td>\n<td data-col-size=\"xl\">Normal distribution \u03bc,\u03c3<\/td>\n<td data-col-size=\"lg\">normal(170,10,10000)<\/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(names, 200)<\/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 <strong>in place<\/strong><\/td>\n<td data-col-size=\"lg\">shuffle(dataset)<\/td>\n<td data-col-size=\"xs\">Yes (before)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">Final teacher advice (very important)<\/h3>\n<p dir=\"auto\"><strong>Always<\/strong> start your random-related work 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 123, 0, 777 \u2014 any fixed number<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">This tiny habit will save you many hours of confusion later.<\/p>\n<p dir=\"auto\">Where do you want to go next?<\/p>\n<ul dir=\"auto\">\n<li>More exotic distributions (poisson, binomial, beta, gamma\u2026)<\/li>\n<li>Randomness in machine learning (dropout, weight init, data augmentation)<\/li>\n<li>Common bugs &amp; misunderstandings with random numbers<\/li>\n<li>Mini-project: generate synthetic dataset + add noise + visualize<\/li>\n<li>Reproducibility tricks across multiple files\/notebooks<\/li>\n<\/ul>\n<p dir=\"auto\">Just tell me what feels most interesting or useful for you right now! \ud83d\ude80<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Random Numbers in NumPy \u2014 written as if I\u2019m your patient teacher sitting next to you, showing examples on the screen, explaining every important detail, giving realistic use-cases, warning about common mistakes, and helping&#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-2497","post","type-post","status-publish","format-standard","hentry","category-numpy"],"_links":{"self":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2497","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=2497"}],"version-history":[{"count":1,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2497\/revisions"}],"predecessor-version":[{"id":2498,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2497\/revisions\/2498"}],"wp:attachment":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/media?parent=2497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/categories?post=2497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/tags?post=2497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}