🎲 Control Python's randomness


Welcome back to Pandas Daily! Master Python in just 5 minutes a day.

​

Today: Understand Python's "random" module to generate random numbers, create random samples for simulations, make random choices etc. Mini exercise at the end!

​

Before we start, a quick question for you!

Thanks! Now, lets begin!


What is random module and why you should care?

Simply put- random module generates random numbers and random choices. Some use cases:

  • 🎟️ Lotteries & lucky draws β†’ pick a random winner.
  • πŸƒ Shuffling a deck of cards β†’ change the order before dealing.
  • 🎯 A/B testing β†’ divide customers into equal groups without bias.

Lets go into the code

​

πŸ”« Import/Load the module

In:
import random
Out:

​

🧩 Generate a floating (decimal) number between 0 and 1

In:
print(random.random())
Out: 0.7905682961988124

​

🌱 Seeding: To reproduce exact same results (very important as "random" will not give same output)

1️⃣ We produce a random number 'a' once

In:
random.seed(42)
a = random.random()
print(a)
Out: 0.6394267984578837

​

2️⃣ Can we get the same random number again as 'b'?

In:
b = random.random()
print(b)
Out: 0.025010755222666936

​

3️⃣ How to make sure we get the same output every time? Use same seed

In:
random.seed(42)
b = random.random()
print(b)
Out: 0.6394267984578837

Moving on..

​

🎲 Any floating number between two numbers

In:
print(random.uniform(10.5, 20.5))
Out: 13.745401188473625

​

#️⃣ Generate integer in a given range using randint(start, end)

In:
print(random.randint(1, 10))
Out: 7

​

πŸ’ͺ 1 step further - Produce numbers in a given range but only at specific intervals: randrange(start, end, interval)

chose from 0,5,10,15,20

In:
print(random.randrange(0, 20, 5))
Out: 15

​

βš–οΈ Random Choice: When you don't know the right answer in the exam πŸ˜‰

In:
print(random.choice([True, False]))
Out: True

​

πŸ’Έ Another Random Choice: Selecting a fruit or a lottery

In:
items = ["apple", "banana", "cherry"]
print(random.choice(items))
Out: banana

πŸ‹οΈ Mini Exercise (Reply with your code)

​

Use random.choice(["apple", "banana", "cherry"]) twice in the same run.

  • Do you always get the same fruit both times?
  • How do you ensure to get the same fruit both times?

β­πŸ“£ That's it for today! If you liked it, please share it with anyone who will find it useful and share your feedback below 🐼

​

Pandas Daily

Beginner to Expert in Python in just 5 minutes

Read more from Pandas Daily

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. We all agree that dashboards, emails, logs should read like English. Meaning 1.2 million beats 1234567 and 3.1M is more readable than 3145728. Stakeholders scan - and they get pissed if they have to parse messy numbers 😭😭 So today, we explore a third-party humanize library. Format numbers, file sizes, and dates/times into human-readable text. Mini exercise at the end. πŸ₯‡ Ready, Get, Set, Go! Install and...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Amazon reviews, chats and social media text (posts, comments, stories etc) are full of emojis πŸ”₯πŸ”₯ Great for expression! But give a hard time for text interpretation (Amazon, Meta would be going crazy). That is where Python's demoji library comes in. From a given text you can: Extract emojis Remove them if needed Replace with textual meaning Understand sentiment and what not... Let's begin!! Install demoji...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. We all know Python’s basic container data types β€” lists, dictionaries, tuples, sets. "Container" is just a data type that holds multiple items instead of single values. However, many times basics are not enough. Like if you want count of different items in the list you will run a loop like below. In: fruits = ["apple", "banana", "apple", "cherry"] counts = {} for item in fruits: counts[item] =...