Functions
This practical, we're going to build some functions into our system. First, though, let's review the code we have so we're all roughly in the same place. Here's a version of the code that should (broadly) match what you have.
import random
import operator
import matplotlib.pyplot
num_of_agents = 10
num_of_iterations = 100
agents = []
# Make the agents.
for i in range(num_of_agents):
agents.append([random.randint(0,99),random.randint(0,99)])
# Move the agents.
for j in range(num_of_iterations):
for i in range(num_of_agents):
if random.random() < 0.5:
agents[i][0] = (agents[i][0] + 1) % 100
else:
agents[i][0] = (agents[i][0] - 1) % 100
if random.random() < 0.5:
agents[i][1] = (agents[i][1] + 1) % 100
else:
agents[i][1] = (agents[i][1] - 1) % 100
'''
answer = (((agents[0][0] - agents[1][0])**2) + ((agents[0][1] - agents[1][1])**2))**0.5
print(answer)
'''
matplotlib.pyplot.ylim(0, 99)
matplotlib.pyplot.xlim(0, 99)
for i in range(num_of_agents):
matplotlib.pyplot.scatter(agents[i][1],agents[i][0])
matplotlib.pyplot.show()
If you haven't got this code, copy and paste it into your file, deleting what is there (save a copy).
The bit of code we're going to concentrate on is the bit that is commented out: the section that works out how far apart agents are. Let's look at this now.