Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 19, 2021 07:18 am GMT

Learning Python- Intermediate course: Day 11, Random numbers

Today we will cover the random module in Python.

Just like the math and cmath modules, the 'random' module is built in into Python, so we do not need to take any extra efforts to download or install it.

Generating a random number

The random() method in random module generates a floating point number between 0 and 1. Note that Python is case sensitive, hence random() and Random() are different!

import random #import the random modulefor i in range(0,5): n = random.random() print(n)
0.82312109710191690.74958514908275520.19100884879163750.76113873149351550.17622975226933524

But many times we want a number in between a specific range of numbers. This is where the randint() method comes handy. The randint() method generates a integer between a given range of numbers.

import randomfor i in range(0,5): n = random.randint(3,7) print(n)
74367

Note that the input parameters of the randint() gives must be in ascending order only. For example this won't work

import random for i in range(0,5): n = random.randint(7,5) print(n)
Traceback (most recent call last):  File "main.py", line 3, in <module>    n = random.randint(10,7)  File "/usr/lib/python3.4/random.py", line 218, in randint    return self.randrange(a, b+1)  File "/usr/lib/python3.4/random.py", line 196, in randrange    raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))ValueError: empty range for randrange() (10,8, -2)

The random() method is often used in data science and statistical operations to obtain random numbers between 0 to 1. For other applications, in most cases the randint() function becomes useful

Typing 'random' every time is just a waste right? We can shorten up the code by replacing random with a short keyword, whoch we can use everytime. The syntax for the following is

import random as rd

SO now every time instead of writing random, we can just write rd
Example-

import random as rdfor i in range(0,5): n = rd.randint(1,10) print(n)
51299

List of random numbers.

Many times, we require to have a list of random numbers.

One way to do that is appending random numbers to the list. We first create an empty list and then append the random numbers one by one.

import random as rdrandomlist = []for i in range(0,5): randomlist.append(rd.randint(15,30))print(randomlist)
[23, 15, 26, 26, 18]

Another way is by using the method sample().

The sample() method takes two arguments. One is the list of numbers to choose random numbers from, and the other is the number of random numbers to choose. The examples below will make things clear.

import random as rdrandomlist = rd.sample(range(15, 30), 5)print(randomlist)
[28, 24, 15, 26, 19]

The range() function returns a list of values from the start to the end.

import random as rdrandomlist = rd.sample([2,3,5,7,11,13,17], 5) #Here the sample returns a random number from the list of prime numbers provided.print(randomlist)
[7, 5, 2, 3, 17]

Your suggestions motivate me, so please please please let me know in the comment section if you this part or not. And don't forget to like the post if you did.


Original Link: https://dev.to/aatmaj/learning-python-intermediate-course-day-11-random-numbers-5cnj

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To