Search code examples
pythonrandomsimulationdistributionuniform

In python, how can I replace first value of a series of matrices by a random number?


I want to simulate N people and each for T amount of time, currently I have that the value of p for each person at T=0 is 0. How can I write this, so instead of having zeros at the first time period, I have a random number that is chosen from a distribution with the other values remaining equal?

N = 100
T = 70
p[N,T] 
p[:,0] = 0.0

Like this


Solution

  • If you need to change the 1st value of the 2D array from a value taken from an early defined array you can follow this way. I have added an example for your reference.

    p = [[0, 1, 1, 1, 1, 1], [0, 2, 2, 3, 3, 3], [0, 2, 3, 3, 3, 4]]
    array = [0.6, 0.5, 0.9]
    for i in range(len(p)):
        if p[i][0] == 0:
            p[i][0] = array[i]  # use random() if you want to replace with a random number
    print(p)
    

    output: [[0.6, 1, 1, 1, 1, 1], [0.5, 2, 2, 3, 3, 3], [0.9, 2, 3, 3, 3, 4]]