{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "###
San Jose State University
Department of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2024
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 4.4 Random-Number Generation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### In data science, we may be asked to test the validity of some results by running a simulation. For example, how can we back up the claim that by rolling a fair six-sided die, each of the faces will come up approximately the same number of times as any other face?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Rolling a Six-Sided Die" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Let's write a simulation of rolling a die. The simulation randomly chooses which face to come up with each simulated roll. We need to import the `random` module from the Python Standard Library." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The `random` module's `randrange()` function takes two parameters. When called, it returns a randomly generated integer value from the first argument value up to, but **not** including, the second argument value." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for roll in range(10):\n", " print(random.randrange(1, 7), end=' ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Because the generated values are random, we'll get different results each time we call the function." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for roll in range(10):\n", " print(random.randrange(1, 7), end=' ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Rolling a Six-Sided Die 6,000,000 Times" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Let's simulate rolling a die 6,000,000 times and print how many times each face comes up. It should be approximately 1,000,000 times." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "### Rolling a Six-Sided Die 6,000,000 Times\n", "\n", "\"\"\"Roll a six-sided die 6,000,000 times.\"\"\"\n", "import random\n", "\n", "# face frequency counters\n", "frequency1 = 0\n", "frequency2 = 0\n", "frequency3 = 0\n", "frequency4 = 0\n", "frequency5 = 0\n", "frequency6 = 0\n", "\n", "# 6,000,000 die rolls\n", "for roll in range(6_000_000): # note underscore separators\n", " face = random.randrange(1, 7)\n", "\n", " # increment appropriate face counter\n", " if face == 1:\n", " frequency1 += 1\n", " elif face == 2:\n", " frequency2 += 1\n", " elif face == 3:\n", " frequency3 += 1\n", " elif face == 4:\n", " frequency4 += 1\n", " elif face == 5:\n", " frequency5 += 1\n", " elif face == 6:\n", " frequency6 += 1\n", "\n", "print(f'Face{\"Frequency\":>13}')\n", "print(f'{1:>4}{frequency1:>13}')\n", "print(f'{2:>4}{frequency2:>13}')\n", "print(f'{3:>4}{frequency3:>13}')\n", "print(f'{4:>4}{frequency4:>13}')\n", "print(f'{5:>4}{frequency5:>13}')\n", "print(f'{6:>4}{frequency6:>13}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### In the f-strings of the `print()` statements, the `>` in a format specification says to right-justify the value." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Seeding the Random-Number Generator for Reproducibility" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The `random` module uses an algorithm to generate the values, and so they are **pseudorandom**. True random values are found in natural phenomena, such as the number of raindrops to hit a windowpane during a storm.\n", "#### The algorithm uses a **seed** value to kick off calculating the pseudorandom sequence. The seed is different every time. For example, it can be a value calculated from the current time.\n", "#### When debugging a program that uses pseudorandom values, you might want the same \"random\" sequence each time you run the program. You can call the function `random.seed()` and explitly set a seed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "random.seed(32)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for roll in range(10):\n", " print(random.randrange(1, 7), end=' ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Continue generating pseudorandom values from seed 32." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for roll in range(10):\n", " print(random.randrange(1, 7), end=' ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Restart generating pseudorandom values from seed 32 and get the same sequence." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "random.seed(32)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for roll in range(10):\n", " print(random.randrange(1, 7), end=' ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **EXAMPLE:** Use a `for` statement, `randrange()`, and a conditional expression to simulate 20 coin flips, displaying `H` for heads and `T` for tails all on the same line, each separated by a space. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for _ in range(20):\n", " face = random.randrange(0, 2)\n", " ht = 'H' if face == 0 else 'T'\n", " print(f'{ht:2}', end='')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### In this example, we don't care about the control variable in the `for` loop (it's only for counting), so we use the dummy `_` variable." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "##########################################################################\n", "# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n", "# Pearson Education, Inc. All Rights Reserved. #\n", "# #\n", "# DISCLAIMER: The authors and publisher of this book have used their #\n", "# best efforts in preparing the book. These efforts include the #\n", "# development, research, and testing of the theories and programs #\n", "# to determine their effectiveness. The authors and publisher make #\n", "# no warranty of any kind, expressed or implied, with regard to these #\n", "# programs or to the documentation contained in these books. The authors #\n", "# and publisher shall not be liable in any event for incidental or #\n", "# consequential damages in connection with, or arising out of, the #\n", "# furnishing, performance, or use of these programs. #\n", "##########################################################################\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Additional material (C) Copyright 2023 by Ronald Mak" ] } ], "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.11.5" } }, "nbformat": 4, "nbformat_minor": 4 }