{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a969ffee",
   "metadata": {},
   "source": [
    "# 模块random:随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6de6a81e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ab9235c5",
   "metadata": {},
   "source": [
    "函数`random.randint()`可以产生一个随机整数。产生一个1到9之间的随机整数:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "83602a1e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.randint(1, 9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0156834a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.randint(1, 9)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "679ca711",
   "metadata": {},
   "source": [
    "函数`random.random()`可以生成一个在0到1之间的随机数:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bcf5a6ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9131366648833286"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.random()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ff79fcf3",
   "metadata": {},
   "source": [
    "函数`random.choice()`可以从一个序列中随机选择一个元素:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ef9aec7f",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [2, 3.2, \"abc\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "206b974f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.choice(a)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "56b689d4",
   "metadata": {},
   "source": [
    "函数`random.shuffle()`可以将序列中元素的顺序进行打乱:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "89710409",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = list(range(20))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ac2d2e63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b64797b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "random.shuffle(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3ae3aa56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[5, 1, 9, 3, 0, 16, 17, 13, 10, 12, 7, 4, 6, 18, 15, 8, 2, 14, 19, 11]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e5565ca6",
   "metadata": {},
   "source": [
    "函数random.sample()可以从序列中不放回地随机采样元素,如采样3个元素:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "57c8ff92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[17, 7, 3]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.sample(b, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be03ffa5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}