-2

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.

quamrana
  • 37,849
  • 12
  • 53
  • 71
Jamal
  • 109
  • 2
  • Please provide the expected [MRE - Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results deviate from the ones you expect. We should be able to paste a single block of your code into file, run it, and reproduce your problem. This also lets us test any suggestions in your context. Your code is not minimal. It is missing at least two imports, and is incorrectly indented -- a full tab, if nothing else. – Prune Mar 13 '21 at 20:26
  • We also expect that you will trace the suspect values just before the point of error. – Prune Mar 13 '21 at 20:27
  • You should post your loop and the call to this function. I see nothing much wrong with your function. – quamrana Mar 13 '21 at 20:32
  • This part is wrong: elif counter == 4: array[j] == q – Stefan Mar 13 '21 at 20:37
  • @Prune - i'm sorry I realise my code was very repetitive and unclear. Ive editted it if that helps – Jamal Mar 13 '21 at 20:43
  • @Prune what do you mean by your second comment : "We also expect that you will trace the suspect values just before the point of error". Im new to coding and I dont really know what that means sorry. – Jamal Mar 13 '21 at 20:44
  • Please go through the [intro tour](https://stackoverflow.com/tour), the [help center](https://stackoverflow.com/help) and [how to ask a good question](https://stackoverflow.com/help/how-to-ask) to see how this site works and to help you improve your current and future questions, which can help you get better answers. – Prune Mar 13 '21 at 20:45
  • @Stefan what is wrong with that bit of code specifically? sorry I dont see it but ty for pointing it out to me. – Jamal Mar 13 '21 at 20:45
  • See this lovely reference for [debugging help](https://ericlippert.com/2014/03/05/how-to-debug-small-programs). – Prune Mar 13 '21 at 20:45
  • You seem to need a lot of individual tutorial help. Remember the purpose of this site: to be an archive of questions and answers useful to professional and enthusiast programmers. This is not a general help desk or tutoring service; this is a last-resort forum for things you can't find elsewhere. In short, you're posting to the wrong site. – Prune Mar 13 '21 at 20:47
  • As far as I can tell, your code does what you expect, apart from where you use == instead of = after the last elif – Stefan Mar 13 '21 at 20:50
  • After everyone else did the work of sorting through your code and getting you to ask the right question: "The error I get is that my when I print the storage variable, every element inside it is the same." Yes; this is caused by putting the same element into the list multiple times. Your function does not create a new object but instead modifies the existing one; `return`ing it from a function does **not** make a copy, so you get the same problem as in the linked duplicate. – Karl Knechtel Mar 13 '21 at 20:51

2 Answers2

1

You are presenting the same list instance each time.

my1 = [33,33,34,0,0,0,0,0,0,0]
store = []

for i in range(1000):
    store.append(stochastic(my1[:]))

You should copy the list each time and then store will be filled with different data.

quamrana
  • 37,849
  • 12
  • 53
  • 71
0

I think it's a problem with your loop condition. Not sure as without an MRE I cannot easily test it.

But if Q = 100 - N - M - P , then won't Q + N + M + P always equal 100?

zraw
  • 1
  • 2
  • I didnt think about the fact that these variables can be negative. But I'm not sure its that as the function runs fine on its own and doesn't get stuck in that while loop – Jamal Mar 13 '21 at 20:47
  • Welcome to Stack Overflow. This response should be a comment. Sorry that the reputation system does the weird things that it does. – Karl Knechtel Mar 13 '21 at 20:52