0
0
NumPydata~10 mins

np.random.default_rng() modern approach in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new random number generator using the modern numpy approach.

NumPy
rng = np.random.[1]()
Drag options to blanks, or click blank then click option'
Adefault_rng
Brandint
Crandom
Dseed
Attempts:
3 left
💡 Hint
Common Mistakes
Using older functions like np.random.seed() instead of default_rng()
Trying to call np.random.random() to create a generator
2fill in blank
medium

Complete the code to generate 5 random integers between 1 and 10 using the modern numpy random generator.

NumPy
rng = np.random.default_rng()
random_numbers = rng.integers([1], 11, size=5)
Drag options to blanks, or click blank then click option'
A5
B1
C0
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Setting low to 0 which includes 0 in output
Setting high to 10 which excludes 10
3fill in blank
hard

Fix the error in the code to generate 3 random floats between 0 and 1 using the modern numpy random generator.

NumPy
rng = np.random.default_rng()
random_floats = rng.[1](3)
Drag options to blanks, or click blank then click option'
Arandom
Brandint
Cintegers
Dchoice
Attempts:
3 left
💡 Hint
Common Mistakes
Using integers() which returns integers
Using randint() which is not a method of the generator instance
4fill in blank
hard

Fill both blanks to create a random number generator with a fixed seed and generate 4 random integers from 0 to 9.

NumPy
rng = np.random.[1](12345)
numbers = rng.[2](0, 10, size=4)
Drag options to blanks, or click blank then click option'
Adefault_rng
Bintegers
Crandom
Dseed
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.random.seed() which does not return a generator
Using random() instead of integers() for integers
5fill in blank
hard

Fill all three blanks to create a dictionary of word lengths for words longer than 3 letters using a comprehension and the modern numpy random generator to shuffle the list.

NumPy
words = ['apple', 'bat', 'carrot', 'dog', 'elephant']
rng = np.random.[1]()
rng.shuffle(words)
lengths = {word: [2] for word in words if len(word) [3] 3}
Drag options to blanks, or click blank then click option'
Adefault_rng
Blen(word)
C>
Drandom
Attempts:
3 left
💡 Hint
Common Mistakes
Using random() instead of default_rng()
Using < instead of > in the condition
Using word instead of len(word) for the dictionary values