|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "e2276806", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "## 1.Write a python generate a random number between 1 and 10." |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": 1, |
| 14 | + "id": "6150b99f", |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [ |
| 17 | + { |
| 18 | + "name": "stdout", |
| 19 | + "output_type": "stream", |
| 20 | + "text": [ |
| 21 | + "The random number is: 1\n" |
| 22 | + ] |
| 23 | + } |
| 24 | + ], |
| 25 | + "source": [ |
| 26 | + "import random\n", |
| 27 | + "\n", |
| 28 | + "random_number = random.randint(1, 10)\n", |
| 29 | + "print(\"The random number is:\", random_number)" |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "markdown", |
| 34 | + "id": "dfd8e493", |
| 35 | + "metadata": {}, |
| 36 | + "source": [ |
| 37 | + "## 2.Write a python code to generate a random number between 1 and 6 (inclusive), simulating the roll of a six-sided die." |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "code", |
| 42 | + "execution_count": 2, |
| 43 | + "id": "a583464b", |
| 44 | + "metadata": {}, |
| 45 | + "outputs": [ |
| 46 | + { |
| 47 | + "name": "stdout", |
| 48 | + "output_type": "stream", |
| 49 | + "text": [ |
| 50 | + "You rolled a 4\n" |
| 51 | + ] |
| 52 | + } |
| 53 | + ], |
| 54 | + "source": [ |
| 55 | + "import random\n", |
| 56 | + "\n", |
| 57 | + "roll = random.randint(1, 6)\n", |
| 58 | + "print(\"You rolled a\", roll)" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "markdown", |
| 63 | + "id": "8ed8910c", |
| 64 | + "metadata": {}, |
| 65 | + "source": [ |
| 66 | + "## 3.Write a python code to generate a random float between 0 and 1, using the uniform() function." |
| 67 | + ] |
| 68 | + }, |
| 69 | + { |
| 70 | + "cell_type": "code", |
| 71 | + "execution_count": 3, |
| 72 | + "id": "643caf9b", |
| 73 | + "metadata": {}, |
| 74 | + "outputs": [ |
| 75 | + { |
| 76 | + "name": "stdout", |
| 77 | + "output_type": "stream", |
| 78 | + "text": [ |
| 79 | + "The random float is: 0.38710117692491564\n" |
| 80 | + ] |
| 81 | + } |
| 82 | + ], |
| 83 | + "source": [ |
| 84 | + "import random\n", |
| 85 | + "\n", |
| 86 | + "rand_float = random.uniform(0, 1)\n", |
| 87 | + "print(\"The random float is:\", rand_float)" |
| 88 | + ] |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "markdown", |
| 92 | + "id": "60b229d9", |
| 93 | + "metadata": {}, |
| 94 | + "source": [ |
| 95 | + "## 4.Write a code to generate a random integer between 0 and 9 (inclusive), using the randrange() function." |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "code", |
| 100 | + "execution_count": 4, |
| 101 | + "id": "c5d3ab3f", |
| 102 | + "metadata": {}, |
| 103 | + "outputs": [ |
| 104 | + { |
| 105 | + "name": "stdout", |
| 106 | + "output_type": "stream", |
| 107 | + "text": [ |
| 108 | + "The random integer is: 0\n" |
| 109 | + ] |
| 110 | + } |
| 111 | + ], |
| 112 | + "source": [ |
| 113 | + "import random\n", |
| 114 | + "\n", |
| 115 | + "rand_int = random.randrange(10)\n", |
| 116 | + "print(\"The random integer is:\", rand_int)" |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "cell_type": "markdown", |
| 121 | + "id": "341db6a0", |
| 122 | + "metadata": {}, |
| 123 | + "source": [ |
| 124 | + "## 5. Write a code for randomly select an item from a list of friend using the choice() function." |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "code", |
| 129 | + "execution_count": 5, |
| 130 | + "id": "3a126428", |
| 131 | + "metadata": {}, |
| 132 | + "outputs": [ |
| 133 | + { |
| 134 | + "name": "stdout", |
| 135 | + "output_type": "stream", |
| 136 | + "text": [ |
| 137 | + "The randomly selected friend is: Santosh\n" |
| 138 | + ] |
| 139 | + } |
| 140 | + ], |
| 141 | + "source": [ |
| 142 | + "import random\n", |
| 143 | + "\n", |
| 144 | + "friend = [\"Sunil\", \"Rahul\", \"Santosh\", \"Tushar\", \"Anil\"]\n", |
| 145 | + "rand_frn = random.choice(friend)\n", |
| 146 | + "print(\"The randomly selected friend is:\", rand_frn)" |
| 147 | + ] |
| 148 | + } |
| 149 | + ], |
| 150 | + "metadata": { |
| 151 | + "kernelspec": { |
| 152 | + "display_name": "Python 3 (ipykernel)", |
| 153 | + "language": "python", |
| 154 | + "name": "python3" |
| 155 | + }, |
| 156 | + "language_info": { |
| 157 | + "codemirror_mode": { |
| 158 | + "name": "ipython", |
| 159 | + "version": 3 |
| 160 | + }, |
| 161 | + "file_extension": ".py", |
| 162 | + "mimetype": "text/x-python", |
| 163 | + "name": "python", |
| 164 | + "nbconvert_exporter": "python", |
| 165 | + "pygments_lexer": "ipython3", |
| 166 | + "version": "3.9.12" |
| 167 | + } |
| 168 | + }, |
| 169 | + "nbformat": 4, |
| 170 | + "nbformat_minor": 5 |
| 171 | +} |
0 commit comments