0

I am using Python to generate some data and have some code like this

num = 0
for i in range(6):
    for j in range(6):
        num = random.randint(0,7)
        #some code here

Instead of producing random numbers, it just makes ten random numbers, and then repeats the sequence for the next nine sets (eg. [1,2,5,1,0,0], [1,2,5,1,0,0], ...). When I run this code again later in the program, it will give me a new set of 6 random numbers, but then repeat it for the next nine sets.

What can I do to prevent this from happening?

2
  • You aren't showing enough of your code. Commented Mar 6, 2012 at 2:07
  • The reference to ten numbers doesn't make a lot of sense with the code you have shown. More code is generally better, especially if you're assigning num into a multi-dimensional array. Commented Mar 6, 2012 at 2:12

1 Answer 1

6

Stop initializing your nested lists incorrectly. [[...] * n] is wrong and will give you multiple references to the same list. Use [[...] for x in range(n)] instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, I'm impressed you figured this out without me even showing that part of my code, this fixes the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.