The Random Library in Python

You might need your program to choose a random lucky winner from a list of people participating in a lottery or maybe you want to chooses a random fruit from a list of fruits the doctor made you. There are many use cases for this library and let us see how we can use this library.

Installation and Importing

The library is included with the normal python installation so you don’t need to install using pip

We need to import the library though so let us do that real quick

<span>import</span> <span>random</span>
<span>import</span> <span>random</span>
import random

Enter fullscreen mode Exit fullscreen mode

Important and Common functions

random.choice()

Now that we have imported the library, let us quickly go through some of the common and important functions the library comes with

Let us see random.choice() first
This will pick a random item from any iterable object (list, string etc.)

Example:

<span>import</span> <span>random</span>
<span>list</span> <span>=</span> <span>[</span><span>"banana"</span><span>,</span> <span>"apple"</span><span>,</span> <span>"guava"</span><span>,</span> <span>"cherry"</span><span>,</span> <span>"strawberry"</span><span>,</span> <span>"mango"</span><span>]</span>
<span>print</span><span>(</span><span>random</span><span>.</span><span>choice</span><span>(</span><span>list</span><span>))</span>
<span># Will pick any random fruit from the list and print that </span>
<span>str</span> <span>=</span> <span>"Coronavirus cases have spiked again!!!"</span>
<span>print</span><span>(</span><span>random</span><span>.</span><span>choice</span><span>(</span><span>str</span><span>))</span>
<span># Will pick any random character from the string and print it out </span>
<span>import</span> <span>random</span>

<span>list</span> <span>=</span> <span>[</span><span>"banana"</span><span>,</span> <span>"apple"</span><span>,</span> <span>"guava"</span><span>,</span> <span>"cherry"</span><span>,</span> <span>"strawberry"</span><span>,</span> <span>"mango"</span><span>]</span>
<span>print</span><span>(</span><span>random</span><span>.</span><span>choice</span><span>(</span><span>list</span><span>))</span>
<span># Will pick any random fruit from the list and print that </span>
<span>str</span> <span>=</span> <span>"Coronavirus cases have spiked again!!!"</span>
<span>print</span><span>(</span><span>random</span><span>.</span><span>choice</span><span>(</span><span>str</span><span>))</span>
<span># Will pick any random character from the string and print it out </span>
import random list = ["banana", "apple", "guava", "cherry", "strawberry", "mango"] print(random.choice(list)) # Will pick any random fruit from the list and print that str = "Coronavirus cases have spiked again!!!" print(random.choice(str)) # Will pick any random character from the string and print it out

Enter fullscreen mode Exit fullscreen mode

图片[1]-The Random Library in Python - 拾光赋-拾光赋
So in the above image we can see that each and every time the function is run, it will print out a different value as it is picking out randomly

random.randrange()

Now this is an interesting function and probably the one most are looking for. It basically picks out a random number from a range of numbers

Example:

<span>import</span> <span>random</span>
<span>print</span><span>(</span><span>random</span><span>.</span><span>randrange</span><span>(</span><span>1</span><span>,</span> <span>20</span><span>,</span> <span>2</span><span>))</span>
<span># Now it will print out any random number amongst 1, 3, 5, 7, 9, 11, 13, 15, 17 and 19 </span>
<span>import</span> <span>random</span>

<span>print</span><span>(</span><span>random</span><span>.</span><span>randrange</span><span>(</span><span>1</span><span>,</span> <span>20</span><span>,</span> <span>2</span><span>))</span>
<span># Now it will print out any random number amongst 1, 3, 5, 7, 9, 11, 13, 15, 17 and 19 </span>
import random print(random.randrange(1, 20, 2)) # Now it will print out any random number amongst 1, 3, 5, 7, 9, 11, 13, 15, 17 and 19

Enter fullscreen mode Exit fullscreen mode

There are 3 arguments taken in of which the first two are compulsory and third one is optional and be default 1. But what are the arguments?

  1. The first argument is the number from which the range starts
  2. The second argument is the number where the range ends
  3. The third argument is the step which is the number of numbers to skip while making the list of the numbers

Now does that sound similar?
Yes, this are the exact same arguments take in by the range function which is in-built in Python. The random.randrange() function basically creates a list from the range with the arguments given and then picks out a random.

random.random()

Now this one is a pretty simple one. It returns a float value between 0 and 1

Example:

<span>import</span> <span>random</span>
<span>print</span><span>(</span><span>random</span><span>.</span><span>random</span><span>())</span>
<span>import</span> <span>random</span>

<span>print</span><span>(</span><span>random</span><span>.</span><span>random</span><span>())</span>
import random print(random.random())

Enter fullscreen mode Exit fullscreen mode

random.seed()

Now this one is a bit tough to understand. So the seed function is basically used to save the state of a random function so that it can generate some random numbers on multiple executions of the code on the same machine or on a different machine for a specific seed value. The seed value is the previous random value generated by the generator or if run for the first time, it is set to the current system time.

Example:

<span>import</span> <span>random</span>
<span>random</span><span>.</span><span>seed</span><span>(</span><span>10</span><span>)</span>
<span>print</span><span>(</span><span>"A mapped random number with seed 10 is: "</span> <span>+</span> <span>random</span><span>.</span><span>random</span><span>())</span>
<span>import</span> <span>random</span>

<span>random</span><span>.</span><span>seed</span><span>(</span><span>10</span><span>)</span>
<span>print</span><span>(</span><span>"A mapped random number with seed 10 is: "</span> <span>+</span> <span>random</span><span>.</span><span>random</span><span>())</span>
import random random.seed(10) print("A mapped random number with seed 10 is: " + random.random())

Enter fullscreen mode Exit fullscreen mode

random.shuffle()

As the name of the function suggests, we can use it to shuffle a list i.e. change the position of the items in the list.

Example:

<span>import</span> <span>random</span>
<span>cities</span> <span>=</span> <span>[</span><span>"London"</span><span>,</span> <span>"Bangalore"</span><span>,</span> <span>"Delhi"</span><span>,</span> <span>"Mumbai"</span><span>,</span> <span>"Tokyo"</span><span>,</span> <span>"New York City"</span><span>]</span>
<span>print</span><span>(</span><span>"List before shuffling: "</span><span>)</span>
<span>print</span><span>(</span><span>cities</span><span>)</span>
<span>print</span><span>(</span><span>"List after shuffling: "</span><span>)</span>
<span>random</span><span>.</span><span>shuffle</span><span>(</span><span>cities</span><span>)</span>
<span>print</span><span>(</span><span>cities</span><span>)</span>
<span>import</span> <span>random</span>

<span>cities</span> <span>=</span> <span>[</span><span>"London"</span><span>,</span> <span>"Bangalore"</span><span>,</span> <span>"Delhi"</span><span>,</span> <span>"Mumbai"</span><span>,</span> <span>"Tokyo"</span><span>,</span> <span>"New York City"</span><span>]</span>

<span>print</span><span>(</span><span>"List before shuffling: "</span><span>)</span>
<span>print</span><span>(</span><span>cities</span><span>)</span>
<span>print</span><span>(</span><span>"List after shuffling: "</span><span>)</span>
<span>random</span><span>.</span><span>shuffle</span><span>(</span><span>cities</span><span>)</span>
<span>print</span><span>(</span><span>cities</span><span>)</span>
import random cities = ["London", "Bangalore", "Delhi", "Mumbai", "Tokyo", "New York City"] print("List before shuffling: ") print(cities) print("List after shuffling: ") random.shuffle(cities) print(cities)

Enter fullscreen mode Exit fullscreen mode

random.uniform()

This one is pretty similar to random.randrange() but then returns a float value instead of an integer value and does not take any step in input

Arguments:

  1. Lower Limit – included in generation
  2. Upper Limit – Excluded in generation

Example:

<span>import</span> <span>random</span>
<span>print</span><span>(</span><span>random</span><span>.</span><span>uniform</span><span>(</span><span>10</span><span>,</span> <span>15</span><span>))</span>
<span>import</span> <span>random</span>

<span>print</span><span>(</span><span>random</span><span>.</span><span>uniform</span><span>(</span><span>10</span><span>,</span> <span>15</span><span>))</span>
import random print(random.uniform(10, 15))

Enter fullscreen mode Exit fullscreen mode

random.randint()

Takes 2 arguments as the lower limit and the upper limit. The returned number is between these 2 values and can be anyone of them as well.

Example:

<span>import</span> <span>random</span>
<span>random</span><span>.</span><span>randint</span><span>(</span><span>50</span><span>,</span> <span>100</span><span>)</span>
<span>import</span> <span>random</span>

<span>random</span><span>.</span><span>randint</span><span>(</span><span>50</span><span>,</span> <span>100</span><span>)</span>
import random random.randint(50, 100)

Enter fullscreen mode Exit fullscreen mode

So that was some of the most important and commonly used functions of the random package and that is it for this blog!!!

Link to random package docs if you want to explore more – https://docs.python.org/3/library/random.html

原文链接:The Random Library in Python

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
You must learn a new way to think before you can master a new way to be.
在掌握新方法之前,你必须要先换一种思考方法
评论 抢沙发

请登录后发表评论

    暂无评论内容