From f2f03f5e2df16e6fdca421d9cba735741f1dde1b Mon Sep 17 00:00:00 2001 From: Chihiro Watanabe Date: Fri, 22 May 2026 16:53:13 +0900 Subject: [PATCH] Update rng usage in schelling.md Co-Authored-By: Claude Sonnet 4.6 --- lectures/schelling.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lectures/schelling.md b/lectures/schelling.md index 8032e1050..fff78e54c 100644 --- a/lectures/schelling.md +++ b/lectures/schelling.md @@ -369,15 +369,15 @@ with small probability, is replaced by an agent of the other type.) solution here ```{code-cell} ipython3 -from numpy.random import uniform, randint - n = 1000 # number of agents (agents = 0, ..., n-1) k = 10 # number of agents regarded as neighbors require_same_type = 5 # want >= require_same_type neighbors of the same type +rng = np.random.default_rng() + def initialize_state(): - locations = uniform(size=(n, 2)) - types = randint(0, high=2, size=n) # label zero or one + locations = rng.uniform(size=(n, 2)) + types = rng.integers(0, 2, size=n) # label zero or one return locations, types @@ -414,7 +414,7 @@ def update_agent(i, locations, types): moved = False while not is_happy(i, locations, types): moved = True - locations[i, :] = uniform(), uniform() + locations[i, :] = rng.uniform(), rng.uniform() return moved 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): while current_iter <= max_iter: # Choose a random agent and update them - i = randint(0, n) + i = rng.integers(0, n) moved = update_agent(i, locations, types) if flip_prob > 0: # flip agent i's type with probability epsilon - U = uniform() + U = rng.uniform() if U < flip_prob: current_type = types[i] types[i] = 0 if current_type == 1 else 1