Could anyone help me how to use rand in MATLAB to write this code: \"To initiali
ID: 3866150 • Letter: C
Question
Could anyone help me how to use rand in MATLAB to write this code:
"To initialize your game, randomly distribute a 2D array where each cell has probability 0.1 of being alive and 0.9 of being dead (i.e., your initial condition should be a random mix with roughly 10% 1’s and 90% 0’s)"
Thank you.
In this problem, we’ll be simulating the fate of living cells using the rules from mathematician John Conway’s famous “Game of Life”. Cells exist at each point on a 2D grid and can be in one of two states: alive (1) or dead (0), as shown in the figure below.
In calculating the next generation, an individual cell’s survival depends on the state of its 8 nearest neighbors (vertically, horizontally, and diagonally adjacent cells). Your code should enforce the following classical rules to calculate each successive generation: • A living cell with either 2 or 3 living neighbors survives on to the next generation. • A living cell with fewer than 2 or more than 3 living neighbors does not survive on to the next generation due to isolation or overcrowding, respectively. • A dead cell with exactly 3 live neighbors becomes a living cell in the next generation. You must employ periodic boundary conditions in this problem to allow the grid to “wrap around” onto itself in both the x and y-direction to avoid artificially influencing cells at the edge of the domain (in the simplified figure above, no boundary conditions were considered and non-visible cells were assumed to hold zeros). Refer to the lecture slides on how to implement these boundary conditions. To visualize the results, we’ll be using the MATLAB function imagesc. To create an animation of your results, simply call imagesc for every generation and use the drawnow function to ensure that each timestep is displayed. (a) Demonstrate your method on a grid with num_rows = 150 and num_cols = 200. To initialize your game, randomly distribute a 2D array where each cell has probability 0.1 of being alive and 0.9 of being dead (i.e., your initial condition should be a random mix with roughly 10% 1’s and 90% 0’s). Run your simulation for 300 timesteps (generations) and present both the initial and final distributions in your report (additional images can be included, but be sure to include these two). In a separate figure, plot the number of living cells in your simulation as a function of time. 1
Explanation / Answer
Before you start rand returns a single uniformly distributed random number in the interval (0,1). Once you have an idea what you can get of rand, it will become easy for you to tackle the answer. Am already providing you a solution to the question you asked:
rows = 10;
cols = 5;
arr = zeros(rows, cols);
for i = 1:rows
for j = 1:cols
if rand() >= 0.9 % 10% of rand() call should ideally return value more than 0.9
arr(i, j) = 1;
end
end
end
arr %to print the array
So this is how you can write the code to generate an array rows and cols pre defined and only 10% values equivalent to 1 and remaining 0