Python For Beginner Series|Day-12

MynotesoracleDBA
Geek Culture
Published in
3 min readNov 14, 2022

--

Here we are going to understand the random module

  • On Day 12, we are going to learn about the random module in detail

What is a Randomization?

  • It is any sort of programming language really useful to create games or if we want to get the result randomly.
  • Randomization helps to create computer programs that have a degree of unpredictability, especially in games
  • In order to access the randomness in our program we can import the random module in the code
  • By using a random module we can generate pseudo-random numbers for various aspects.

To access the random module:

import random

The random module methods

  1. seed()

2. getstate()

3. setstate(state_obj)

4. getrandbits(k)

5. randomrange(start,stop,step)

6. randint(a, b)

7. random(a,b)

  • Sometimes large codes are split into small bits of modules where each module is responsible for a different bit of functionality of our program.
  • Let’s see the sample example
  • In this above example, I have imported the random module in python code
  • There is a function name called randint which returns random integer numbers
  • On the randint(a,b) here a is starting value ,b is the end value

Possible output:

  • It displays the result between 1 to 10.
7

9

3

7
  • Similar to generating integers, there are functions that generate random floating point sequences.
  • Here is an example
import random

"""first random is a module after a dot is float random generation function """
random_float=random.random()

#it returns the interval 0 to 1

print(random_float)

Possible output:

  • It returns the value between 0 to 1 which means 0.999 not even 1.
0.6392451910852722
0.09553498185562392
0.5446452971298421
0.7535909981144928

How do create a random decimal number between 0 to 5?

  • It is not that much complex
  • As we knew ,we could get the random float numbers between 0.00000–0.9999 . Hence how did we achieve to generate random numbers between 0.0000–4.9999
  • Here is an example
import random


random_float=random.random()


c=random_float * 5

print(c)

Possible output:

  • We have just multiplied by the random float value by 5, so we are getting the values between 0.000–4.999
1.19688541978746
2.0290389423081967
3.514735688934367
2.246457203434934
3.7127092896879237
  • We could use this random number to achieve the love meter program

Here is our code:

import random

lover_meter=random.randint(0,100)

print(f"your love score is {lover_meter}")
  • In our love meter program we need to get the user name as input and verify the letter count with “true love” based on the result count we will get the love meter score. but using the random number we can easily get the lover score between 0 to 100.

Possible output:

your love score is 95
your love score is 1
your love score is 5
your love score is 44
  • From today onwards we are going to assign the small tasks for end the blog , it will be very helpful to get hands-on.
  • Write a program coin toss program and display the result randomly “Heads / Tails “

Instructions:

  • Import the random module
  • There are many ways of doing this. But to practice what we learnt in the last lesson, you should generate a random number, either 0 or 1. Then use that number to print out Heads or Tails.
  • e.g. 1 means Heads 0 means Tails
  • Share your result in comments.

--

--

MynotesoracleDBA
Geek Culture

As a DBA we are going to write and discuss multiple database administration concepts. “Nothing Grows In Comfort Zone”.