← Back to NumPy Course | Chapter 2: Creating Arrays | Lesson 6 of 7

np.random के Basics

NumPy random numbers generate कर सकता है, जो sample data और simulations के लिए handy है।

In this page:

  1. np.random Basics
Syntax
python
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

python
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. #}
आम गलतियां
  1. seed करना भूल जाना और हर बार अलग output पाना
  2. legacy और Generator APIs mix करना
  3. यह उम्मीद करना कि 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:

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.