# Implementation of stochastic cellular automata

In my problem, I have a lattice with a stochastic cellular automaton. In order to simplify a bit, let's say it is 1D. In my system, each node can be type A, B or C. A way to represent the system and operate fast over it is using an integer 64-bit variable, working bitwise.

Let me give an example of implementation for a system with 4 nodes. Let's say I have that nodes 0 and 3 are in state A, node 1 is in B, and 2 is in C, then:

A -> 1001 -> A = 9
B -> 0100 -> B = 4
C -> 0010 -> C = 2


You see that 3 integers can represent the whole state of the system. I can update the system with a single bit-wise operation. This is used a lot in cellular automatons.

But, now, suppose I have reactions such as

$$A+B\overset{\mu}{\rightarrow}2B$$ $$B\overset{\nu}{\rightarrow}C$$

I want to simulate this reactions in a synchronous way using a discrete time $\Delta t$. I can obtain the number of B individuals in the next timestep with:

B = A && r


where $r$ is a random number such that the bit at position $j$ is 1 with probability $p_j=1-\exp(-\mu\Delta t\cdot b_j)$, where $b_j$ is the number of B neighbours of individual $j$. This is, it is 1 with the probability of executing reaction 1.

My question is: is there an efficient method to obtain $r$?

Not sure if I understood correctly, but I think that if your parameters µ and $\Delta t$ are constant during all the simulations and for all nodes, you can generate a list of possible $p_j$ values in the beginning of the simulations. Because you have a limited possiblity of $b_j$ values (you have maximum 8 neighbors in a 2D grid if you look at first-neighborhood, so you can do an array of 9 possibles $p_j$ values).
Then you generate a random number with uniform law each time and compare if it's smaller than its corresponding $p_j$ value from the list.