@@ -369,15 +369,15 @@ with small probability, is replaced by an agent of the other type.)
369369solution here
370370
371371``` {code-cell} ipython3
372- from numpy.random import uniform, randint
373-
374372n = 1000 # number of agents (agents = 0, ..., n-1)
375373k = 10 # number of agents regarded as neighbors
376374require_same_type = 5 # want >= require_same_type neighbors of the same type
377375
376+ rng = np.random.default_rng()
377+
378378def 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
420420def 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