I’m new to Elixir, I’m really excited about the idea of having many concurrent processes communicating through messages. Unfortunately, most of the examples I came across only show the case of generating two processes communicating with each other through knowing each other’s PIDs and I’m not sure if I can treat the PIDs as index-able ids.
So I was wondering how can I spawn for e.g 100 concurrent processes, each process should have an identifiable index for example: process1_id=1, process2_id=2…, and process100_id=100. Each process stores a list of ids of 5 other processes and has an internal function that can randomly produce a number from 1 to 100 upon receiving a “hello” message from another process in its list. Then depending on the generated number each process should send a “hello” message to another process.
To put the algorithm more concretely:
1- first generate and spawn 100 concurrent processes
2- upon generation, each process should internally hold and initialize a list of 5 random numbers from 1 to 100
e.g process1_list=[2, 81, 14, 20, 70], process2_list=[31, 21,3,99,10] and so on…
3- each process should continually sleep for 10 seconds then generate a random number from 1 to 100 and accordingly send a “hello” message to another process that have an id equal to the generated number.
4- when a process receives a “hello” message from another process, it should check their internal list of the initially 5 generated number if the id of the sender is in the list then the receiver should immediately (i.e not after 10s) generate another random number from 1 to 100 and send a “hello” message to another process. If the id of the sender was not in the list then the receiver should do nothing immediately but as default should generate a random number from 1-100 after each 10 second pass.
I deeply appreciate if anyone can guide me on how can I implement the above efficiently in Elixir
Many thanks in advance