{"id":2512,"date":"2026-02-02T09:39:59","date_gmt":"2026-02-02T09:39:59","guid":{"rendered":"https:\/\/demo.materiamedica.net\/demo6\/?p=2512"},"modified":"2026-02-02T09:39:59","modified_gmt":"2026-02-02T09:39:59","slug":"chapter-8-uniform-distribution","status":"publish","type":"post","link":"https:\/\/demo.materiamedica.net\/demo6\/chapter-8-uniform-distribution\/","title":{"rendered":"Chapter 8: Uniform Distribution"},"content":{"rendered":"<h3 dir=\"auto\">1. What is the Uniform distribution really?<\/h3>\n<p dir=\"auto\">The <strong>uniform distribution<\/strong> is the simplest and most intuitive continuous probability distribution.<\/p>\n<p dir=\"auto\">It means:<\/p>\n<blockquote dir=\"auto\">\n<p dir=\"auto\">Every value inside a certain interval is <strong>equally likely<\/strong> to occur.<\/p>\n<\/blockquote>\n<p dir=\"auto\">There are <strong>no favorite values<\/strong> \u2014 no peak, no bell shape, no skew. It\u2019s completely flat between the minimum and maximum.<\/p>\n<p dir=\"auto\">There are two main flavors we care about:<\/p>\n<ul dir=\"auto\">\n<li><strong>Continuous uniform<\/strong> \u2192 any real number between a and b is equally likely<\/li>\n<li><strong>Discrete uniform<\/strong> \u2192 equally likely integers (like rolling a fair die)<\/li>\n<\/ul>\n<p dir=\"auto\"><strong>Key parameters<\/strong> (only two):<\/p>\n<ul dir=\"auto\">\n<li><strong>a<\/strong> = minimum value (lower bound)<\/li>\n<li><strong>b<\/strong> = maximum value (upper bound)<\/li>\n<\/ul>\n<p dir=\"auto\">The probability density is constant: <strong>f(x) = 1 \/ (b \u2212 a)<\/strong> for a \u2264 x \u2264 b <strong>f(x) = 0<\/strong> everywhere else<\/p>\n<h3 dir=\"auto\">2. Mental picture \u2014 how it looks<\/h3>\n<p dir=\"auto\">Imagine you drop a dart completely randomly on a number line between 10 and 30. Every single point between 10 and 30 has exactly the same chance of being hit.<\/p>\n<p dir=\"auto\">The histogram looks like a perfect <strong>rectangle<\/strong>:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>text<\/div>\n<div>\n<pre tabindex=\"0\"><code>\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n   \u2502                             \u2502\r\n   \u2502                             \u2502\r\n   \u2502                             \u2502   \u2190 flat = uniform\r\n   \u2502                             \u2502\r\n   \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n      10                        30<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">3. Generating uniform random numbers in NumPy<\/h3>\n<p dir=\"auto\">NumPy gives you two very commonly used functions:<\/p>\n<h4 dir=\"auto\">A. np.random.rand() \u2014 standard uniform [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 array\r\nprint(np.random.rand(8))\r\n\r\n# 2D array \u2014 very common shape\r\nprint(np.random.rand(5, 6))\r\n\r\n# Large array for visualization\r\nu = np.random.rand(100000)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">B. np.random.uniform(low, high, size=&#8230;) \u2014 custom interval<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code># Between 0 and 100 (like random percentages)\r\nprint(np.random.uniform(0, 100, 10).round(1))\r\n\r\n# Between -5 and 5 (symmetric around zero)\r\nsymmetric = np.random.uniform(-5, 5, 50000)\r\n\r\n# Temperatures between 18\u00b0C and 32\u00b0C\r\ndaily_temps = np.random.uniform(18, 32, 365)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">4. Visualizing the uniform distribution (very important)<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 5))\r\n\r\n# Standard uniform [0,1)\r\nsns.histplot(np.random.rand(80000), bins=60, stat=\"density\",\r\n             color=\"skyblue\", alpha=0.8, ax=ax1)\r\nax1.set_title(\"Uniform distribution [0, 1)\", fontsize=13)\r\nax1.set_xlabel(\"Value\")\r\nax1.set_ylabel(\"Density\")\r\nax1.set_xlim(0, 1)\r\n\r\n# Custom uniform [15, 45]\r\ncustom = np.random.uniform(15, 45, 80000)\r\nsns.histplot(custom, bins=60, stat=\"density\",\r\n             color=\"teal\", alpha=0.8, ax=ax2)\r\nax2.set_title(\"Uniform distribution [15, 45]\", fontsize=13)\r\nax2.set_xlabel(\"Value\")\r\nax2.set_ylabel(\"Density\")\r\n\r\nplt.tight_layout()\r\nplt.show()<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>What you should always see<\/strong>:<\/p>\n<ul dir=\"auto\">\n<li>Flat top (almost perfect rectangle when sample size is large)<\/li>\n<li>Sharp drop to zero outside [a, b]<\/li>\n<li>Density height = <strong>1 \/ (b \u2212 a)<\/strong><\/li>\n<\/ul>\n<h3 dir=\"auto\">5. Very common real-world use cases (you will meet these often)<\/h3>\n<h4 dir=\"auto\">Use case 1 \u2013 Generating random test values \/ mock data<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code># Random prices between 9.99 and 99.99\r\nprices = np.random.uniform(9.99, 99.99, size=500)\r\n\r\n# Random percentages \/ probabilities\r\nprobabilities = np.random.rand(1000)   # [0,1)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Use case 2 \u2013 Random initialization \/ starting points<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code># Random starting weights for simple models\r\nweights = np.random.uniform(-0.1, 0.1, size=(100, 30))\r\n\r\n# Random split points for time series cross-validation\r\nsplit_points = np.random.uniform(0.1, 0.9, size=50)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Use case 3 \u2013 Simulating uniform sensor readings \/ background noise<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code># Background noise in some measurement device (uniform between -2 and +2)\r\nnoise = np.random.uniform(-2, 2, size=10000)\r\n\r\nsignal = 50 + noise<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Use case 4 \u2013 Random sampling of categories \/ discrete uniform<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>options = ['A', 'B', 'C', 'D', 'E']\r\nrandom_choices = np.random.choice(options, size=10000)\r\n\r\nsns.countplot(x=random_choices, palette=\"Set2\")\r\nplt.title(\"Discrete uniform \u2013 equal probability\")\r\nplt.show()<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">6. Important properties &amp; formulas (write these down)<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"lg\">Property<\/th>\n<th data-col-size=\"md\">Value \/ Formula<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"lg\">Range<\/td>\n<td data-col-size=\"md\">[a, b] or [0, 1) for rand()<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Mean (expected value)<\/td>\n<td data-col-size=\"md\"><strong>(a + b) \/ 2<\/strong><\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Variance<\/td>\n<td data-col-size=\"md\"><strong>(b \u2212 a)\u00b2 \/ 12<\/strong><\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Standard deviation<\/td>\n<td data-col-size=\"md\"><strong>(b \u2212 a) \/ \u221a12<\/strong><\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Probability density<\/td>\n<td data-col-size=\"md\"><strong>1 \/ (b \u2212 a)<\/strong> inside interval<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Cumulative distribution<\/td>\n<td data-col-size=\"md\"><strong>(x \u2212 a) \/ (b \u2212 a)<\/strong> for a \u2264 x \u2264 b<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Quick check:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>a, b = 15, 45\r\ndata = np.random.uniform(a, b, 100000)\r\n\r\nprint(\"Mean (should be ~30)    :\", data.mean().round(3))\r\nprint(\"Std  (should be ~8.66)  :\", data.std().round(3))\r\nprint(\"Theoretical std          :\", (b - a) \/ np.sqrt(12))<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">7. Common student questions &amp; confusions<\/h3>\n<p dir=\"auto\"><strong>Q: Is uniform the same as random?<\/strong><\/p>\n<p dir=\"auto\">No. Uniform means <strong>equal probability density<\/strong> in the interval. \u201cRandom\u201d is a much broader term \u2014 normal, binomial, exponential are also random, but not uniform.<\/p>\n<p dir=\"auto\"><strong>Q: Why do we often see np.random.rand() instead of uniform(0,1)?<\/strong><\/p>\n<p dir=\"auto\">rand() is just a convenient shortcut for uniform(0,1). They give exactly the same numbers (when seeded the same).<\/p>\n<p dir=\"auto\"><strong>Q: Why does the histogram never look perfectly flat?<\/strong><\/p>\n<p dir=\"auto\">Because it\u2019s a <strong>sample<\/strong>. With 100 values \u2192 looks bumpy. With 100,000 values \u2192 almost perfect rectangle.<\/p>\n<h3 dir=\"auto\">Summary \u2013 Uniform Distribution Quick Reference<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"md\">Property<\/th>\n<th data-col-size=\"lg\">Value \/ Formula<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"md\">Shape<\/td>\n<td data-col-size=\"lg\">Flat rectangle<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Defined by<\/td>\n<td data-col-size=\"lg\">a (min), b (max)<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Mean<\/td>\n<td data-col-size=\"lg\">(a + b)\/2<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Variance<\/td>\n<td data-col-size=\"lg\">(b \u2212 a)\u00b2 \/ 12<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">NumPy standard [0,1)<\/td>\n<td data-col-size=\"lg\">np.random.rand(size)<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Custom interval<\/td>\n<td data-col-size=\"lg\">np.random.uniform(a, b, size)<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Most common use cases<\/td>\n<td data-col-size=\"lg\">mock data, initialization, test values, background noise, random sampling<\/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>Whenever you need something \u201ccompletely fair \/ no preference \/ equal chance\u201d within a range<\/strong> \u2192 think uniform.<\/p>\n<p dir=\"auto\"><strong>Whenever you see a flat histogram in real data<\/strong> (rare but happens in some sensors, quantization, etc.) \u2192 think uniform or discrete uniform.<\/p>\n<p dir=\"auto\">Where would you like to go next?<\/p>\n<ul dir=\"auto\">\n<li>Difference between continuous vs discrete uniform<\/li>\n<li>How uniform is used in random number generation algorithms<\/li>\n<li>Transforming uniform to other distributions (inverse transform sampling)<\/li>\n<li>Comparing uniform vs normal vs exponential side by side<\/li>\n<li>Realistic mini-project: generate mock customer data + prices + timestamps<\/li>\n<\/ul>\n<p dir=\"auto\">Just tell me what interests you most right now! \ud83d\ude0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. What is the Uniform distribution really? The uniform distribution is the simplest and most intuitive continuous probability distribution. It means: Every value inside a certain interval is equally likely to occur. There are&#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-2512","post","type-post","status-publish","format-standard","hentry","category-numpy"],"_links":{"self":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2512","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=2512"}],"version-history":[{"count":1,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2512\/revisions"}],"predecessor-version":[{"id":2513,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2512\/revisions\/2513"}],"wp:attachment":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/media?parent=2512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/categories?post=2512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/tags?post=2512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}