<jupyter_start><jupyter_text>Learning Goals* You will apply everything you've learned up to this point to simulate random walks in various dimensions and visualize them.* You will appreciate the role of random fluctuations in molecular diffusion.  Diffusion![](images/synapse.png)  What cuases the solute to diffuse?![](images/diffusion.jpg)  Discrete random walk in one dimensionWrite a program that simulates a one-dimensional random walk amongst 100 discrete sites where each of 1000 identical and independent particles has equal probability to move one site in either direction at each time step. Treat the edges as reflecting (i.e. the particle moves back the other direction with 100% probability). Start the particles distributed uniformly across five sites centered on the 20th site and observe how they distribute as time progresses (i.e. keep track of all particle locations at each time point for later visualization). Make the probability of going left-vs-right a variable so that you can play around with it.![](images/randomwalk1d.gif)  Step 1: What data structure(s) will you use to keep track of all the particles for all time steps of the simulation?* 100 discrete sites* 1000 identical and independent particles8 min.  Step 2: Initialize your data structure(s) based on the starting conditions.Start the particles distributed uniformly across five sites centered on the 20th site8 min.<jupyter_code>import numpy as np
particles = np.zeros((2000, 100))
particles[0,18:23] = 200

particles[0, 16:25]<jupyter_output><empty_output><jupyter_text>Step 3: Write pseudocode for how you will run the simulation.15 min.<jupyter_code>FOR each time step:
    FOR each site:
        get # particles at site
        FOR each particle at site:
            get random number
            IF random number < 0.5 and can or have to move left:
                move left
            ELSE:
                move right<jupyter_output><empty_output><jupyter_text>Step 4: Translate your pseudocode to code.10 min.<jupyter_code>for t in range(1999):
    for s in range(100):
        numParticleAtSite = particles[t,s]
        for i in range(int(numParticleAtSite)):
            rn = np.random.random()
            if (rn < 0.5 and s > 0) or (s == 99):
                particles[t+1,s-1] += 1
            else:
                particles[t+1,s+1] += 1<jupyter_output><empty_output><jupyter_text>Step 5: Visualize your simulation.8 min.<jupyter_code>import matplotlib.pyplot as plt

plt.imshow(particles, aspect='auto', cmap=plt.get_cmap('nipy_spectral_r'))
plt.xlabel('Site #')
plt.ylabel('Time Step');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07401755055322395, 'fraction_numerical': 0.02823349866463182, 'mean_word_length': 3.846580406654344, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '520548', 'license': 'Unlicense', 'n_tokens_mistral': 786, 'n_tokens_neox': 708, 'n_words': 307, 'path': 'lectures/Feb-04-random_walk/random_walks.ipynb', 'repo_name': 'satvikmoj/NEU-365P-385L-Spring-2021', 'size': '2621'}