Function to create random matrix, then a simulation. *EDIT* Create two functions
Update: Animation of fire-spreading:
ClearAll[initState, vNNeighbors, step, iterationList]
initState[b_, i_, j_, pos_: Automatic] := ReplacePart[
RandomChoice[{1/(1 + b), b/(1 + b)} -> {0, 1}, {i, j}],
(pos /. Automatic -> RandomChoice[Tuples[{Range@i, Range@j}]]) -> 2]
vNNeighbors[dim_: {30, 30}] := AdjacencyList[NearestNeighborGraph@Tuples@Range@dim, #]&
step = MapAt[Min[2, 2 #] &, #, vNNeighbors[][Position[#, 2]]] &;
iterationList[nmax_: Automatic][b_, i_, j_, pos_: Automatic] :=
NestList[step, initState[b, i, j, pos], nmax /. Automatic -> Max[i, j]]
Example:
SeedRandom[1]
startpos = {15, 5};
{i, j} = {30, 30};
Manipulate[ListAnimate[MatrixPlot[#, Mesh -> All, Frame -> False] & /@
iterationList[][b, i, j, startpos], Paneled -> False],
{{b, .6}, .6, 1.6, .2}]
Alternatively, you can use FixedPointList
to generate the list of arrrays:
ClearAll[FPList]
FPList[b_, i_, j_, pos_: Automatic] := FixedPointList[step, initState[b, i, j, pos]]
SeedRandom[1]
Manipulate[ListAnimate[MatrixPlot[#, Mesh -> All, Frame -> False] & /@
FPList[b, i, j, startpos], Paneled -> False],
{{b, .6}, .6, 1.6, .2}]
Original answer:
ClearAll[initState]
initState[b_, i_, j_, pos_: Automatic] := ReplacePart[
RandomChoice[{1/(1 + b), b/(1 + b)} -> {0, 1}, {i, j}],
(pos /. Automatic -> RandomChoice[Tuples[{Range@i, Range@j}]]) -> 2]
Default position of 2
is random:
SeedRandom[1]
MatrixPlot[initState[.6, 30, 30], Mesh -> All]
Position of 2
is {10, 15}
SeedRandom[1]
MatrixPlot[initState[.6, 30, 30, {10, 15}], Mesh -> All]
Animations:
Position of 2
changes randomly in every iteration:
SeedRandom[1]
frames1 = Table[MatrixPlot[initState[b, 30, 30], Mesh -> All,
Frame -> False], {b, Range[.6, 1.8, .2]}];
Export["animation1.gif", frames1]
Position of 2
is remains fixed at a random value over iterations:
SeedRandom[1]
frames2 = With[{pos2 = RandomChoice[Tuples[{Range@30, Range@30}]]},
Table[MatrixPlot[initState[b, 30, 30, pos2], Mesh -> All,
Frame -> False], {b, Range[.6, 1.8, .2]}]];
Export["animation2.gif", frames2]