import random import math class Node(): def __init__(self, name): # They start by all sending at the same time self.time_to_send = 0 # To keep track of which nodes have successfully transmitted self.successful = False # In case naming nodes is helpful for debugging self.name = name def retransmit(self, curr_time): pass def simulate_collision(N): node_list = [] for i in range(N): node_list.append(Node(f'Node {i}')) nodes_sending_at_slot = [] curr_time = 0 # Iterate through time slots and check which nodes are scheduled to send while not all([node.successful for node in node_list]): for node in node_list: if node.time_to_send == curr_time: nodes_sending_at_slot.append(node) # Node sends successfully if it is the only one sending in a slot if len(nodes_sending_at_slot) == 1: nodes_sending_at_slot[0].successful = True nodes_sending_at_slot = [] curr_time += 1 def main(): N = 5 # Number of nodes simulate_collision(N) if __name__ == '__main__': main()