np.random के Basics
NumPy random numbers generate कर सकता है, जो sample data और simulations के लिए handy है।
In this page:
Syntax
rng = np.random.default_rng(seed)
rng.random(size)
rng.integers(low, high, size)
rng.normal(loc, scale, size)
np.random Basics
Modern तरीका np.random.default_rng(seed) है, जो random, integers, और normal जैसे methods वाला एक Generator return करता है। एक seed set करना results reproducible बनाता है। np.random.rand जैसे Legacy functions अभी भी काम करते हैं और बाद में cover होते हैं।
Note:
जब आपको हर run same numbers चाहिए हों हमेशा seed करें, जैसे tutorials और tests में।
उदाहरण: np.random basics
import numpy as np
rng = np.random.default_rng(42)
print(rng.random(3).round(3))
print(rng.integers(1, 10, size=5))
# Output:
# [0.774 0.439 0.859]
# [1 7 2 1 5]
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. #}
आम गलतियां
- seed करना भूल जाना और हर बार अलग output पाना
- legacy और Generator APIs mix करना
- यह उम्मीद करना कि integers() का upper bound inclusive है
चैप्टर सारांश
- default_rng एक Generator बनाता है
- Reproducibility के लिए Seed करें
- random 0 से 1 में floats देता है
- integers high value exclude करता है
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: