← Back to Python Course | Chapter 12: Standard Library | Lesson 4 of 9

Python random मॉड्यूल

random मॉड्यूल आपके program को dice रोलने या cards शफल करने जैसी मौके की choices बनाने देता है। यह games और surprises के लिए काम आता है।
Syntax
python
import random

random.random()
random.randint(start, stop)
random.choice(sequence)

Random Numbers बनाना

random मॉड्यूल एक seeded internal state से pseudo-random numbers generate करता है, जिसका मतलब है कि अगर आप random.seed() से seed fix कर दें तो sequence दोबारा reproduce की जा सकती है -- जो debugging या reproducible tests के लिए उपयोगी है।

random.random() [0.0, 1.0) में एक float लौटाता है, जबकि random.uniform(a, b) आपकी बताई हुई किसी भी custom range में एक float लौटाता है।

उदाहरण: Generating Random Numbers

python
import random
random.seed(1)  # fixes the sequence so results are reproducible
print(random.random())  # float in [0.0, 1.0)
print(random.uniform(1, 10))  # float in a custom range

Random Integers

random.randint(a, b) एक integer लौटाता है जहाँ a और b दोनों possible results होते हैं (एक inclusive range), जो range() के exclusive upper bound के आदी लोगों को चौंका देता है।

random.randrange() इसके बजाय range() के exclusive-upper-bound व्यवहार को follow करता है, इसलिए जो boundary semantics आपको चाहिए उसके हिसाब से सही वाला चुनें।

उदाहरण: Random Integers

python
import random
random.seed(1)
print(random.randint(1, 6))  # inclusive on both ends
print(random.randrange(0, 10, 2))  # exclusive upper bound, like range()

Random Choices चुनना

random.choice() किसी non-empty sequence में से एक random element चुनता है, जहाँ हर element के चुने जाने की समान संभावना होती है।

random.choices() *replacement के साथ* कई elements चुनता है, यानी एक ही element एक से ज़्यादा बार चुना जा सकता है -- यह वैकल्पिक weights भी स्वीकार करता है ताकि कुछ elements के चुने जाने की संभावना ज़्यादा हो।

उदाहरण: Picking Random Choices

python
import random
random.seed(1)
items = ["a", "b", "c"]
print(random.choice(items))  # one random element
print(random.choices(items, k=2))  # multiple elements, with replacement

Lists को Shuffle करना

random.shuffle() किसी list के elements का क्रम उसी जगह randomize कर देता है और None लौटाता है, इसलिए इसे एक स्वतंत्र statement के रूप में ही call करना चाहिए, assign करके नहीं -- एक आम शुरुआती गलती है x = random.shuffle(my_list) लिखना और shuffled list की बजाय None वापस पाना।

उदाहरण: Shuffling Lists

python
import random
random.seed(1)
items = [1, 2, 3, 4]
random.shuffle(items)  # shuffles in place and returns None
print(items)

बिना Replacement के Sampling

random.sample() *बिना replacement के* बताई गई संख्या में elements चुनता है, जिससे result में duplicates नहीं होते और original sequence अछूता रहता है।

इसे तब इस्तेमाल करें जब आपको सच में random subset चाहिए हो, जैसे deck में से unique cards बाँटना, जहाँ choices() में मिलने वाली संभावित repeats गलत होंगी।

उदाहरण: Sampling Without Replacement

python
import random
random.seed(1)
deck = [1, 2, 3, 4, 5]
print(random.sample(deck, 3))  # 3 unique elements, no duplicates, deck itself untouched
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.