You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now, when we call the function `generate_data()`, we pass `np.random.uniform`
381
+
Now, when we call the function `generate_data()`, we pass `rng.uniform`
380
382
as the second argument.
381
383
382
384
This object is a *function*.
383
385
384
-
When the function call `generate_data(100, np.random.uniform)` is executed, Python runs the function code block with `n` equal to 100 and the name `generator_type` "bound" to the function `np.random.uniform`.
386
+
When the function call `generate_data(100, rng.uniform)` is executed, Python runs the function code block with `n` equal to 100 and the name `generator_type` "bound" to the function `rng.uniform`.
385
387
386
-
* While these lines are executed, the names `generator_type` and `np.random.uniform` are "synonyms", and can be used in identical ways.
388
+
* While these lines are executed, the names `generator_type` and `rng.uniform` are "synonyms", and can be used in identical ways.
387
389
388
390
This principle works more generally---for example, consider the following piece of code
389
391
@@ -507,7 +509,7 @@ factorial(4)
507
509
508
510
The [binomial random variable](https://en.wikipedia.org/wiki/Binomial_distribution) $Y \sim Bin(n, p)$ represents the number of successes in $n$ binary trials, where each trial succeeds with probability $p$.
509
511
510
-
Without any import besides `from numpy.random import uniform`, write a function
512
+
Using `rng = np.random.default_rng()`, write a function
511
513
`binomial_rv` such that `binomial_rv(n, p)` generates one draw of $Y$.
512
514
513
515
```{hint}
@@ -527,12 +529,12 @@ If $U$ is uniform on $(0, 1)$ and $p \in (0,1)$, then the expression `U < p` eva
527
529
Here is one solution:
528
530
529
531
```{code-cell} python3
530
-
from numpy.random import uniform
532
+
rng = np.random.default_rng()
531
533
532
534
def binomial_rv(n, p):
533
535
count = 0
534
536
for i in range(n):
535
-
U = uniform()
537
+
U = rng.uniform()
536
538
if U < p:
537
539
count = count + 1 # Or count += 1
538
540
return count
@@ -558,7 +560,7 @@ Second, write another function that does the same task except that the second ru
558
560
559
561
- If a head occurs `k` or more times within this sequence, pay one dollar.
560
562
561
-
Use no import besides `from numpy.random import uniform`.
563
+
Use `rng = np.random.default_rng()` to generate random numbers.
562
564
563
565
```{exercise-end}
564
566
```
@@ -573,15 +575,15 @@ Here's a function for the first random device.
573
575
574
576
575
577
```{code-cell} python3
576
-
from numpy.random import uniform
578
+
rng = np.random.default_rng()
577
579
578
580
def draw(k): # pays if k consecutive successes in a sequence
579
581
580
582
payoff = 0
581
583
count = 0
582
584
583
585
for i in range(10):
584
-
U = uniform()
586
+
U = rng.uniform()
585
587
count = count + 1 if U < 0.5 else 0
586
588
print(count) # print counts for clarity
587
589
if count == k:
@@ -601,7 +603,7 @@ def draw_new(k): # pays if k successes in a sequence
0 commit comments