.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/builtin_modules/_random_module.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_builtin_modules__random_module.py: ============ 2.10 random ============ .. important:: This lesson is still under development. .. GENERATED FROM PYTHON SOURCE LINES 10-14 .. code-block:: default import random .. GENERATED FROM PYTHON SOURCE LINES 15-18 random -------------- We can use random function to generate a random number between 0 and 1. .. GENERATED FROM PYTHON SOURCE LINES 18-20 .. code-block:: default print(random.random()) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.5075855301555765 .. GENERATED FROM PYTHON SOURCE LINES 21-22 every time we call ``random.random()``, the result will be different and therefore unpredictable .. GENERATED FROM PYTHON SOURCE LINES 22-25 .. code-block:: default print(random.random()) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.6626777902671553 .. GENERATED FROM PYTHON SOURCE LINES 26-28 .. code-block:: default print(random.random()) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.48008835976152975 .. GENERATED FROM PYTHON SOURCE LINES 29-33 randint -------------- If we want to generate a random integer between two ranges, we can make use of randint. .. GENERATED FROM PYTHON SOURCE LINES 33-36 .. code-block:: default print(random.randint(1, 10)) .. rst-class:: sphx-glr-script-out .. code-block:: none 10 .. GENERATED FROM PYTHON SOURCE LINES 37-41 choice() -------------- If we want to select a value randomly from a given sequence, we can make use of random.choice function. .. GENERATED FROM PYTHON SOURCE LINES 41-44 .. code-block:: default random.choice([1,2,3,4]) .. rst-class:: sphx-glr-script-out .. code-block:: none 3 .. GENERATED FROM PYTHON SOURCE LINES 45-47 .. code-block:: default random.choice([1,2,3,4]) .. rst-class:: sphx-glr-script-out .. code-block:: none 4 .. GENERATED FROM PYTHON SOURCE LINES 48-52 choices() -------------- However, if we want to select more than one value, but randomly, from a sequence, we can then make use of random.choices. .. GENERATED FROM PYTHON SOURCE LINES 52-55 .. code-block:: default random.choices([1,2,3,4,5, 6, 7]) .. rst-class:: sphx-glr-script-out .. code-block:: none [4] .. GENERATED FROM PYTHON SOURCE LINES 56-58 .. code-block:: default random.choices([1,2,3,4,5], k=2) .. rst-class:: sphx-glr-script-out .. code-block:: none [1, 2] .. GENERATED FROM PYTHON SOURCE LINES 59-62 sample() -------------- The sample function is very much similar to ``choices`` function. .. GENERATED FROM PYTHON SOURCE LINES 62-68 .. code-block:: default random.sample([1,2,3,4,5], k=2) dakoos = ['generals', 'siyasatdan', 'bureaucracy', 'saiths', 'anchors'] random.sample(dakoos, k=2) .. rst-class:: sphx-glr-script-out .. code-block:: none ['siyasatdan', 'bureaucracy'] .. GENERATED FROM PYTHON SOURCE LINES 69-80 seed() -------------- Suppose someone is making use of ``random.random()`` function in his script and getting some results. If you use the same script, your result will be different because we have seen that ``random()`` function generates a different number upon every call. How can we make use of random() function and still get reproducible results? The seed function from random module is used to generate reproducible results. Let's generate five random numbers using random.random() function inside a for loop. .. GENERATED FROM PYTHON SOURCE LINES 80-84 .. code-block:: default for i in range(5): print(random.random()) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.04033176172530484 0.7732993145762757 0.6686300310142389 0.07223078199362987 0.8422513070269098 .. GENERATED FROM PYTHON SOURCE LINES 85-88 The above five numbers are `completely` random in the sense that we can not predict, what would be the next number. If we call the random.random() again, the newly generated random numbers will again be random and unpredictable. .. GENERATED FROM PYTHON SOURCE LINES 88-92 .. code-block:: default for i in range(5): print(random.random()) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.19703798316077914 0.7438889505923774 0.8505202699780908 0.05682085241108681 0.39353025225729354 .. GENERATED FROM PYTHON SOURCE LINES 93-94 Now, Let's set the random seed and then generate the random numbers using random.random() .. GENERATED FROM PYTHON SOURCE LINES 94-100 .. code-block:: default random.seed(313) for i in range(5): print(random.random()) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.5646032330638283 0.9693332662214504 0.42674278078547345 0.8722737879866462 0.19282451151232616 .. GENERATED FROM PYTHON SOURCE LINES 101-105 We have generated 5 random numbers so far after setting the ranomd seed with 313. If we reset the random seed again, the next five random numbers that will be generated will be exactly same as the first five random numbers that were generated (above) when we had set the seed to 313 initially. .. GENERATED FROM PYTHON SOURCE LINES 105-111 .. code-block:: default random.seed(313) for i in range(5): print(random.random()) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.5646032330638283 0.9693332662214504 0.42674278078547345 0.8722737879866462 0.19282451151232616 .. GENERATED FROM PYTHON SOURCE LINES 112-114 This means, every time we set the random seed, the generated numbers will be random but reproducible. .. GENERATED FROM PYTHON SOURCE LINES 116-118 **Question**: (Can you) Predict the random numbers generated as a result of following code? .. GENERATED FROM PYTHON SOURCE LINES 118-123 .. code-block:: default # random.seed(92) # for i in range(5): # print(random.random()) .. GENERATED FROM PYTHON SOURCE LINES 124-128 Consider for example a function which uses random numbers in it using ``random.random()``. Now every time we run the function, the results will be different. This means the output of our function will not be reproducible. We can set the random seed either globally (at the start of script) or locally (inside the function) in order to make our results reproducible. .. GENERATED FROM PYTHON SOURCE LINES 130-132 **Question**: What will be the last value printed by the following code? .. GENERATED FROM PYTHON SOURCE LINES 132-137 .. code-block:: default # random.seed(313) # for i in range(7): # print(random.random()) .. GENERATED FROM PYTHON SOURCE LINES 138-141 Setting the random seed does not only affect random.random() function, but if affects all functions of random module. Consider for example random.choice function. .. GENERATED FROM PYTHON SOURCE LINES 141-145 .. code-block:: default for _ in range(3): print(random.choice(dakoos)) .. rst-class:: sphx-glr-script-out .. code-block:: none siyasatdan generals bureaucracy .. GENERATED FROM PYTHON SOURCE LINES 146-151 .. code-block:: default random.seed(313) for _ in range(3): print(random.choice(dakoos)) .. rst-class:: sphx-glr-script-out .. code-block:: none anchors siyasatdan saiths .. GENERATED FROM PYTHON SOURCE LINES 152-157 .. code-block:: default random.seed(313) for _ in range(3): print(random.choice(dakoos)) .. rst-class:: sphx-glr-script-out .. code-block:: none anchors siyasatdan saiths .. GENERATED FROM PYTHON SOURCE LINES 158-160 shuffle() -------------- .. GENERATED FROM PYTHON SOURCE LINES 160-166 .. code-block:: default print(dakoos) random.shuffle(dakoos) print(dakoos) .. rst-class:: sphx-glr-script-out .. code-block:: none ['generals', 'siyasatdan', 'bureaucracy', 'saiths', 'anchors'] ['siyasatdan', 'bureaucracy', 'anchors', 'generals', 'saiths'] .. GENERATED FROM PYTHON SOURCE LINES 167-171 .. code-block:: default random.shuffle(dakoos) print(dakoos) .. rst-class:: sphx-glr-script-out .. code-block:: none ['generals', 'saiths', 'bureaucracy', 'anchors', 'siyasatdan'] .. GENERATED FROM PYTHON SOURCE LINES 172-178 **Question**: Write a function called ``pseudo_shuffle``, which suffles the `dakoos` list, but the order of values in the returned/shuffled list is always same. This means calling ``pseudo_shuffle(dakoos)`` multile times return same order in the list. It should be noted that the function ``pseudo_shuffle`` must make use of ``random.suffle`` function inside it. .. GENERATED FROM PYTHON SOURCE LINES 180-182 randrange -------------- .. GENERATED FROM PYTHON SOURCE LINES 182-185 .. code-block:: default random.randrange(1, 10) .. rst-class:: sphx-glr-script-out .. code-block:: none 3 .. GENERATED FROM PYTHON SOURCE LINES 186-189 .. code-block:: default random.randrange(1, 10, step=2) .. rst-class:: sphx-glr-script-out .. code-block:: none 7 .. GENERATED FROM PYTHON SOURCE LINES 190-192 getstate() -------------- .. GENERATED FROM PYTHON SOURCE LINES 194-196 setstate() -------------- .. GENERATED FROM PYTHON SOURCE LINES 198-199 distributions -------------- .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.007 seconds) .. _sphx_glr_download_auto_examples_builtin_modules__random_module.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/AtrCheema/python-seekho/master?urlpath=lab/tree/notebooks/auto_examples/builtin_modules/_random_module.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: _random_module.py <_random_module.py>` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: _random_module.ipynb <_random_module.ipynb>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_