Skip to content

Commit f2f03f5

Browse files
Update rng usage in schelling.md
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent c162978 commit f2f03f5

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

lectures/schelling.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -369,15 +369,15 @@ with small probability, is replaced by an agent of the other type.)
369369
solution here
370370

371371
```{code-cell} ipython3
372-
from numpy.random import uniform, randint
373-
374372
n = 1000 # number of agents (agents = 0, ..., n-1)
375373
k = 10 # number of agents regarded as neighbors
376374
require_same_type = 5 # want >= require_same_type neighbors of the same type
377375
376+
rng = np.random.default_rng()
377+
378378
def initialize_state():
379-
locations = uniform(size=(n, 2))
380-
types = randint(0, high=2, size=n) # label zero or one
379+
locations = rng.uniform(size=(n, 2))
380+
types = rng.integers(0, 2, size=n) # label zero or one
381381
return locations, types
382382
383383
@@ -414,7 +414,7 @@ def update_agent(i, locations, types):
414414
moved = False
415415
while not is_happy(i, locations, types):
416416
moved = True
417-
locations[i, :] = uniform(), uniform()
417+
locations[i, :] = rng.uniform(), rng.uniform()
418418
return moved
419419
420420
def plot_distribution(locations, types, title, savepdf=False):
@@ -446,12 +446,12 @@ def sim_random_select(max_iter=100_000, flip_prob=0.01, test_freq=10_000):
446446
while current_iter <= max_iter:
447447
448448
# Choose a random agent and update them
449-
i = randint(0, n)
449+
i = rng.integers(0, n)
450450
moved = update_agent(i, locations, types)
451451
452452
if flip_prob > 0:
453453
# flip agent i's type with probability epsilon
454-
U = uniform()
454+
U = rng.uniform()
455455
if U < flip_prob:
456456
current_type = types[i]
457457
types[i] = 0 if current_type == 1 else 1

0 commit comments

Comments
 (0)