Automatically translated.View original post

🎲 Basic Python: Create a random number

Number randomness is a very important basic function in programming. Whether it's doing games, simulations, or even random passwords! 🔑

💡 How to generate random numbers in Python:

1.Import Module: The random module must be imported first (random import) because it is a finished module attached to Python.

2.Use the ranged () function:

• random. Randrange (start, stop)

• This function will randomize integer numbers from start to stop (excluding stop).

• Example in Image: random. Randrange (1, 10) will only random numbers from 1 to 9.

🚀 What can I do with it?

• Create a number game

• Dice roll simulation

• Randomly select prizes from List

• Create OTP code

Very simple and useful. Try it together.

# data # ai # python # data aanalyst # learnonlemon8

2025/11/5 Edited to

... Read moreเวลาเราค้นหา “สุ่มเลข 1-10” ใน Python สิ่งที่หลายคนพลาดบ่อยสุดคือ “ขอบเขตของเลข” ค่ะ เพราะ randrange(start, stop) จะสุ่มตั้งแต่ start ไปจนถึง “ก่อนถึง stop” (ไม่รวม stop) ดังนั้นถ้าต้องการ 1–10 จริงๆ ต้องเขียนเป็น random.randrange(1, 11) ไม่ใช่ (1, 10) 1) เริ่มจาก import random ก่อนใช้งานต้องพิมพ์บรรทัดนี้ไว้ด้านบนของไฟล์เสมอ: import random 2) สุ่มเลข 1-10 แบบตรงๆ ถ้าอยากได้ 1 ถึง 10: - random.randrange(1, 11) หรืออีกวิธีที่อ่านง่าย: - random.randint(1, 10) (อันนี้ “รวม” เลขปลายทางให้เลย) ส่วนตัวเวลาสอนหรือทำโจทย์ “สุ่มตัวเลข1-10” ฉันมักใช้ randint เพราะกันงงเรื่องปลายทางไม่ถูกรวม แต่ถ้าต้องการกำหนด step หรือช่วงแบบยืดหยุ่น randrange จะตอบโจทย์กว่า 3) หากต้องการสร้าง “ชุดตัวเลขสุ่ม” และกำหนดค่าต่ำ-สูงสุดได้ ควรใช้ฟังก์ชันอะไร? - ถ้าต้องการสุ่ม “หลายครั้ง” ภายในช่วงที่กำหนด: ใช้ randrange / randint ในลูป ตัวอย่าง สุ่ม 5 ตัวเลขในช่วง 1–10: nums = [random.randrange(1, 11) for _ in range(5)] - ถ้าต้องการ “ไม่ซ้ำกัน” (เช่น สุ่มรางวัลโดยไม่ให้คนเดิมได้ซ้ำ): ใช้ random.sample ตัวอย่าง สุ่มเลขไม่ซ้ำ 5 ตัวจาก 1–10: nums = random.sample(range(1, 11), 5) 4) อยากทำเหมือน app สุ่มตัวเลข (กดแล้วได้ผลลัพธ์) แม้ไม่ทำแอปเต็มๆ ก็ทำเวอร์ชันง่ายๆ ในคอนโซลได้ เช่น สุ่มแล้วแสดงผลทันที: print(random.randrange(1, 11)) หรือถ้าอยากให้กด Enter เพื่อสุ่มเรื่อยๆ ก็วนลูปเพิ่มได้ 5) ตัวอย่างใช้งานที่หยิบไปใช้ได้เลย - ทอยลูกเต๋า: random.randrange(1, 7) - สุ่มของรางวัลจากลิสต์: prizes = ["A", "B", "C"] winner = random.choice(prizes) สรุปจำง่ายๆ: randrange(1, 10) ได้ 1–9 แต่ถ้าต้องการ 1–10 ให้ขยับ stop เป็น 11 หรือใช้ randint(1, 10) ไปเลย จะตรงกับสิ่งที่คนส่วนใหญ่ค้นหาและคาดหวังค่ะ

Related posts

A person wearing headphones sits at a desk with a computer displaying a graph, a plant, and a glowing yellow lamp. The image has text overlay 'Learn Python in 50 DAYS' and 'lemon8 @compskyy'.
A blurred background of a laptop with text overlay outlining the Python learning schedule for Day 1-20, covering programming basics, operations, strings, variables, and control structures.
A blurred background of a desk with a computer screen, featuring text overlay detailing the Python learning schedule for Day 21-40, including functions, modules, exceptions, files, and functional programming.
Learn Python in 50 Days!!
🐍If you want to learn how to code and you don't know what programming language to start with, then I would highly recommend Python! It's a super popular language in the industry and you can do so much with it. It is very beginner friendly compared to most languages and the fields you could
CompSkyy

CompSkyy

600 likes

Ball python care guide
I am no expert, but this is basic ball python care & experience. If you have any other questions feel free to comment 🖤 #ballpython #ballpythonmom #lemon8bookclub #Lemon8Diary #reptilecare
Jas

Jas

240 likes

A flat lay shows a notebook open to a handwritten 'realistic learning plan' with daily objectives and solutions, next to a laptop and sticky notes. A hand holds a pen, ready to write.
A digital note titled 'Creating a Realistic Learning Plan' outlines the first steps: Define Your Goal, Assess Your Current Knowledge, and Break Down the Goal, each with examples.
A digital note titled 'Creating a Realistic Learning Plan' details the next steps: Create a Timeline, Identify Learning Resources, and Allocate Time, each with examples.
How to create a *realistic* learning plan 📅 ⏰
Creating a realistic learning plan depends on your specific goals, interests, and available time. Here's a general framework that you can customize based on your needs: 🎉 Don't forget to Celebrate Milestones! Acknowledge and celebrate your achievements along the way to stay motivated.
teal.days

teal.days

1640 likes

Why use SQL/Python for Data Analysis, (not excel?)
Hello everyone! SQL and Python are favored over Excel for data analysis due to their efficiency with large datasets and automation capabilities. Python, using libraries like Pandas, ensures reproducible workflows, while SQL handles complex data manipulations with ease in relational databases. Pytho
Yun Jung

Yun Jung

91 likes

Useful Python libraries for Data Analysts
Hello everyone! I wanted to highlight key Python libraries for data analysts that I find very useful! To start with, Pandas library excels in data manipulation with DataFrames and Series. NumPy supports efficient handling of large arrays and mathematical operations. Matplotlib offers versatile plot
Yun Jung

Yun Jung

63 likes

Why analysts should use Python for Data Processing
Hello Data Analysts! I wanted to highlight Python's efficiency in data cleaning and preprocessing. Python libraries like Pandas and NumPy offer methods to handle missing data, transform data types, and encode categorical variables like one-hot encoding. Tools such as drop_duplicates() ensure da
Yun Jung

Yun Jung

43 likes

✿˖° how to create a form on Notion
‧°𐐪♡𐑂°‧₊ ☁️ If you’re still using Google forms or other form creators, you might want to check this out instead! Notion recently came out with new features including forms, where you can create and send out forms, surveys, quizzes and more straight from your workspace! This is a mini tutorial
peachiesuga ♡

peachiesuga ♡

65 likes

A desk setup featuring a Dell monitor, a white CHERRY keyboard, and a pink mouse, with the text overlay "easy coding projects for beginners" and a "LEARN MORE" button.
A white note card listing "Easy coding projects for beginners" including a to-do list app, personalized calculator, weather app, and simple quiz game, with descriptions for each.
An outdoor scene with a wide road lined by palm trees and buildings under a clear blue sky, with the text overlay "Comment 💻 if you'll try these projects!"
Easy Coding Projects!!
If you want to learn how to code then these are the perfect beginner projects! Here are some fun and easy projects to build your skills: To-Do List App: Create a simple app where you can add, edit, and delete tasks—perfect for practicing JavaScript or Python. ✅ Personalized Calculator: Build a
CompSkyy

CompSkyy

172 likes

how I got started learning Python 🐍💻👾
I’ve been learning Python this summer to prepare for my research lab and courses in the fall. Programming isn’t required for my major, so I’m taking the time to learn it alone. The biggest tip I can give is specific goals from the beginning! Speaking from experience, this will save a lot of time an
reagan

reagan

46 likes

A pink-themed desk setup featuring a computer monitor displaying a Notion wishlist with columns for item, priority, category, cost, and links. The screen shows items like 'dream experience' and 'nice to have' with prices. The text overlay reads 'How to create a Wishlist in Notion SWIPE TO VIEW'.
A step-by-step guide on creating a new table view database in Notion. It shows typing '/table' to add a basic table block, then selecting 'Table view' from the database options, and finally creating a new blank table.
Instructions on adding columns to the Notion wishlist. It shows how to edit property names like 'item' and add new columns by clicking '+'. It also illustrates selecting different property types such as Text, Number, Select, Multi-select, Date, and Checkbox.
how to create a Wishlist on notion! 🍰
°𐐪♡𐑂°‧₊ 🍵 Here’s a step-by-step tutorial on how I’ve set up my simple notion Wishlist to track things I want to do/buy! I put everything into a table-view database, with columns for what the item is, the price, a link to it, with optional fields such as priority, why you want it, the item categor
peachiesuga ♡

peachiesuga ♡

184 likes

Create your first no-code app in minutes!
The moment you build your first tiny app, something clicks. You stop overthinking and start creating. Maybe it’s an app you use personally. Maybe it’s turning an old PDF into an interactive dashboard. Maybe it’s the tool your audience has been wishing someone would make. No-code apps
Bria | Social Media Marketing

Bria | Social Media Marketing

11 likes

5 Best YouTube Channels for Python & Data Science
#pythonforbeginners #datasciencetips #learntocode #youtubelearning #techcareers
Aysha

Aysha

111 likes

A woman with curly hair holds a microphone, presenting a title card that reads 'Data Analysis with Python For Beginners'.
A woman with curly hair holds a microphone, presenting an introductory text about simple steps for learning Python for Data Analysis.
A woman with curly hair holds a microphone, presenting a list of four steps for beginners to learn Python for Data Analysis.
Beginner Friendly Tips For Learning Python for Dat
If I was to learn Python for Data Analysis as a beginner, I would start with the basics, I would learn: Variables Data Types Loops and Functions. There's no Python without libraries and these are the most used for Data Analysis: 👉 Pandas - Data Manipulation 👉 Numpy - Numerical Comput
NeLo

NeLo

77 likes

🔥 Assign Multiple Values in Python (Easy!)
🔥 Assign Multiple Values in Python (Easy!) Assigning multiple values in Python makes your code shorter, cleaner, and more readable 💻 Perfect trick for beginners and interview prep 🚀 Save this post and follow for daily Python tips 🐍 #Python #LearnPython #PythonBasics #PythonTips
TechKeysX

TechKeysX

0 likes

💻Try Linear Regression Modeling in Python
Since we’ve covered basic concepts like mean, median, correlation and linear regression, it is time we practiced using Python. Please refer to the pictures in this post for detailed coding and data display. 🖋️Get data set from Kaggle I downloaded the Titanic data set from the famous data scie
Capital&Crypto

Capital&Crypto

13 likes

Data Analyst - Start a career today
1. Master Essential Tools and Skills - Why It’s Crucial: Employers prioritize technical proficiency for handling and visualizing data. - Focus Areas: - Excel: For basic data analysis and pivot tables. - SQL: To query and manage large datasets (a must-have skill). - Python/
vedha | career tips (tech) 👩‍

vedha | career tips (tech) 👩‍

222 likes

A computer screen displays a game interface with Python-like code on the right and a farming simulation on the left, illustrating how the game teaches coding. The text overlay asks, "this game teaches you how to code!?"
A computer screen shows a game with multiple code windows and a farming simulation. An overlay explains the game's language is similar to Python, suitable for beginners and experienced coders with complex challenges.
A computer screen displays a game's text editor and entity list, including "can_harvest" and "range." Overlays state "the farmer was replaced" and explain the game teaches coding basics to build a foundation for further self-study.
this cute app can teach you how to code python
ive always wanted to learn how to code more than html and css, something on the complex side. it always has seemed intimidating until i stumbled upon this game/app where the task are gamified, so its seems more fun to explore rather than formal courses. are you willing to give it a shot? #l
yuuki

yuuki

372 likes

Global Variables in Python 🌍
Global variables allow data to be shared across your program, but misuse can lead to bugs. Learn when and how to use them correctly in Python 🐍 #Python #PythonBasics #LearnPython #Coding #Programming
TechKeysX

TechKeysX

0 likes

Python Libraraies for Web scraping
#python #webscraping #freshlemon8 #usreels #paulalytics
Paulalytics

Paulalytics

1 like

💻Use Python Like a Quant
I’ve listed the Python packages I use in work. I’ll elaborate a bit more about them. Pandas and numpy are the core tools for data processing and analysis. Talib is very useful for indicator calculation. Moving average, MACD or RSI calculation are easily handled by entering parameters to my li
Capital&Crypto

Capital&Crypto

11 likes

Tips for having a happy ball python
#lemon8diarychallenge I love my reptiles🥰 #petsnake #ballpython #reptilekeeper #reptile
jordynemilee

jordynemilee

49 likes

A close-up of a hand showcasing elaborate, long sculpted nails. Each nail features unique 3D designs, including snake-like textures, speckled green patterns, brown and cream swirls, and black and white zebra stripes wrapped around raised elements. The artistic nails display intricate details and varied patterns.
Python nails 💅 🔥🐍
These belong in a art museum 🔥🔥🔥😮‍💨 #fyp #nails #NailArt
taurusangel33

taurusangel33

34 likes

A character sheet portrait of a smiling woman, a 52-year-old mom of seven, surrounded by elements reflecting her organized, resilient life. It highlights her journey balancing family, personal growth, and learning technology like Python, JavaScript, and SQL, with daily routines, goals, and affirmations for managing anxiety and ADHD.
🌹 From Python, JavaScript and SQL Books to Code Dreams: This Is Me
🌹 From Budget Books to Code Dreams: This Is Me This beautiful character-sheet portrait captures me being organized, resilient, and focused on growth. I am surrounded by planners, notebooks, coding references, roses, and inspirational reminders, I represent the perfect blend of determination and
mirandaperry05

mirandaperry05

1 like

Harley, my beautiful ball python.
#snake #ballpython #royalpython #pythonsnake #daffodils
Cheryl 🐍

Cheryl 🐍

1 like

A dark-themed image titled 'Python Text Processing - Part 1' displays Python code that splits a paragraph into words, counts them, and prints the first five. Below the code, the output shows the original paragraph, the total word count (21), and the list of the first five words: ['makes', 'it', 'easy', 'to', 'work'].
Python Mini-Lesson Part 1: Count Words in a Prgh
Learning Python one mini-lesson at a time 🐍 In Part 1, we break down how to count words in any paragraph using just a few lines of Python. We split the text, count the total number of words, and print the first five. Save this if you’re learning Python! paragraph = "makes it easy to work
Hicham Regragui

Hicham Regragui

2 likes

My ball python gets a new home!
I can’t wait to get him all set up, he’s gonna love it and I’m just excited to see him explore his new home😍🥰 #ballpython #ballpythons #snake #reptile #reptilesoflemon8
Magan Celine

Magan Celine

2 likes

See more