Alright, so today I wanna share something kinda cool I’ve been messing around with: this “63 lucky number” thing. Basically, I was trying to figure out a way to generate a “lucky” number, and for some reason, 63 kept popping into my head. Don’t ask me why, it just did!
So, where did I even start? Well, first, I just googled “how to generate random numbers.” Yeah, super basic, I know. Found a bunch of stuff about using the random
module in Python. Looked easy enough.
I fired up my code editor – VS Code, nothing fancy – and started typing. First, I imported the random
module. That part’s pretty straightforward:
import random
Next, I wanted to create a function that would generate a random number between 1 and 63 (since, you know, 63 is the goal here). I figured, let’s keep it simple. Used . Here’s what I ended up with:
def generate_lucky_number():
return *(1, 63)
Okay, so far so good. Now, here’s where I got a little more ambitious. I didn’t just want one lucky number. I wanted a bunch of them. So, I decided to write a little loop that would generate a list of lucky numbers.
I decided to make the number of lucky numbers configurable. I threw in a variable named num_lucky_numbers
and set it to, say, 10.
def generate_lucky_numbers(num_lucky_numbers):
lucky_numbers = []
for _ in range(num_lucky_numbers):
lucky_*(*(1, 63))
return lucky_numbers
Cool, right? Now I could generate a list of 10 (or any number) of lucky numbers. I ran the function a few times to test it out. It seemed to be working as expected.
But then I started thinking, “What if I want to weight the randomness a little bit?” What if I wanted the number 63 to appear more often than the other numbers? That’s when things got interesting.
I dove back into the random
module documentation and discovered . This lets you specify a list of “weights” for each element in a list, so some elements are more likely to be chosen than others.
This was perfect! I created a list of numbers from 1 to 63, and then assigned a weight of, like, 5 to the number 63, and a weight of 1 to all the other numbers. That way, 63 would be five times more likely to be chosen than any other number.
Here’s the code I ended up using:
def generate_weighted_lucky_numbers(num_lucky_numbers):
numbers = list(range(1, 64)) # Numbers from 1 to 63
weights = [1] 63 # All numbers initially have a weight of 1
weights[62] = 5 # Give number 63 a weight of 5
lucky_numbers = *(numbers, weights=weights, k=num_lucky_numbers)
return lucky_numbers
I ran this a few times, and sure enough, 63 showed up a lot more often. Success!
Finally, just to make it a little more user-friendly, I wrote a quick bit of code to print out the lucky numbers in a nice format.
lucky_numbers = generate_weighted_lucky_numbers(20) # Generate 20 weighted numbers
print("Your lucky numbers are:")
for number in lucky_numbers:
print(number, end=" ") # Print each number followed by a space
print() # Print a newline at the end.
So, yeah, that’s pretty much it. I started with a super simple idea – generating random numbers – and ended up playing around with weighted randomness. It was a fun little project, and I learned a few new tricks along the way.
Key Takeaways:
- The
random
module in Python is your friend. - is great for simple random number generation.
- is awesome for weighted randomness.
Give it a try yourself! Maybe you can come up with an even better way to generate “lucky” numbers. Who knows, maybe you’ll actually get lucky!