I have a function that has a random aspect to it. The function takes an array which contains 10 integers and depending on how many non-zero integers are in the array, it randomly changes their value.
My function works as I want it to and whenever I run it normally, it provides a different output.
However I would like to run the function in a loop and store all my random outputs. When I try this, all my outputs end up being the same. I dont know why that is seeing as the function works when it is outside of the loop.
How can I get the function to return a different value for every iteration of the loop so that I can store them.
My function code is below in case its needed (sorry if its not clean):
def stochastic(array):
count = np.count_nonzero(array)
n = 0
m = 0
p = 0
q = 0
r = 0
counter = 0
if count == 3:
while n+m+p != 100:
n = random.randint(28,36)
m = random.randint(28,36)
p = 100 - (n+m)
if count == 4:
while n+m+p+q != 100:
n = random.randint(20,41)
m = random.randint(20,41)
p = random.randint(20,41)
q = 100 - (n+m+p)
if count == 5:
while n+m+p+q+r != 100:
n = random.randint(5,41)
m = random.randint(5,41)
p = random.randint(5,41)
q = random.randint(5,41)
r = 100 - (n+m+p+r)
for j in range(len(array)):
if array[j] != 0:
counter += 1
if counter == 1:
array[j] = n
elif counter == 2:
array[j] = m
elif counter == 3:
array[j] = p
elif counter == 4:
array[j] == q
elif counter == 5:
array[j] == r
if count == counter:
counter = 0
return array
print(stochastic(my2))
my1 = [33,33,34,0,0,0,0,0,0,0]
store = []
for i in range(1000):
store.append(stochastic(my1))
Edit: Sorry I realised my function was atrocious and had lots of repetition.
I changed my function to remove repetition of code (i also added a counter == 5 case).
I also added my loop.
The error I get is that my when I print the storage variable, every element inside it is the same.