-5

The question is, place exactly 12 zeros at random positions in a 6x6 python matrix, made using for loops. Now I have no idea how to do random integer that can make some random numbers the same... I need help for this

l = []
for i in range(6):
   l.append([])
      for j in range(6):
            l[i].append(???)
print(l)
quamrana
  • 37,849
  • 12
  • 53
  • 71
  • 6
    Start by fixing the indentation of your code. Secondly, where is the matrix? The description seems to suggest you would be given a matrix and then need to place zeroes in it. Thirdly, your code doesn't include `randint`... – trincot Feb 02 '22 at 21:24
  • 1
    Welcome to Stack Overflow! Please provide a [example], and _show your research effort_. Please see [how to ask and answer homework questions](https://meta.stackoverflow.com/q/334822), the [help], and [ask] for more information. There is an infinite amount of information out there already on how to use `randint`; what don't you understand? – Sylvester Kruin Feb 02 '22 at 21:25
  • 1
    Why do you say that you `have no idea how to do random integer` when you mention `randint` in the title? – quamrana Feb 02 '22 at 21:26
  • 2
    Put 12 zeros into a list and fill the rest with… something… then shuffle it. – deceze Feb 02 '22 at 21:26
  • Choose 12 random numbers from 0 to 35. X is N//6,Y is N%6. – Tim Roberts Feb 02 '22 at 21:26
  • 1
    @deceze that may actually be harder, since its a 2D list – Dillon Davis Feb 02 '22 at 21:27
  • It wasn't copied I just wrote it here :/ – Stefannnnn Feb 02 '22 at 21:29
  • Make a list of all the coordinates in the matrix. Select 12 random elements from the list, using `random.choices()`. Then store 0 in those coordinates. – Barmar Feb 02 '22 at 21:32
  • @Barmar does that work with these kinds of matrixes? This isn't a prebuilt one it's made using for loops.. – Stefannnnn Feb 02 '22 at 21:34
  • The question doesn't say anything about creating the matrix, it just says to insert zeroes at random positions in an existing matrix. – Barmar Feb 02 '22 at 21:35
  • @Barmar my bad I forgot to mention that explicitly, I assumed it was seeable through the code – Stefannnnn Feb 02 '22 at 21:38
  • I don't always trust the code, because if you knew what you were doing you wouldn't be posting here :) – Barmar Feb 02 '22 at 21:39
  • Okay, so there are 36 places to choose from, and you want to choose 12 of them, without replacement. Do you see anything in the documentation for the `random` module that might be helpful? Alternately: you have 12 of one kind of value and 24 of the other kind. If you just start by putting them in sequence however, can you see something in the random module that would rearrange them into random places? – Karl Knechtel Feb 02 '22 at 21:44

2 Answers2

2

Create a list of 12 zeroes and 24 random non-zero values. Shuffle the list to get the zeroes into random places. Then loop through the list creating the matrix from it.

nums = [0] * 12 + [random.randint(1, 4) for _ in range(24)]
random.shuffle(nums)

matrix = []
for i in range(6):
    row = []
    for j in range(6):
        row.append(nums[i*6 + j])
    matrix.append(row)
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • See also: https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks – Karl Knechtel Feb 02 '22 at 21:44
  • @KarlKnechtel I didn't use a better method to split the list because the assignment specifically says to use `for` loops. – Barmar Feb 02 '22 at 21:45
0

Here is one way to place 12 random 0's in a 6 by 6 list (Biased)

import random
l = []
num_zeros = 0
for i in range(6):
    l.append([])
    for j in range(6):
        if random.randint(0,1) and num_zeros < 12:
            l[-1].append(0)
            num_zeros += 1
        else:
            l[-1].append(random.randint(1,4))

Another option:

l = []
for i in range(12):
    l.append(0)
for i in range(24):
    l.append(random.randint(1,4))
random.shuffle(l)
chunks = [l[x:x+6] for x in range(0, len(l), 6)]
print(chunks)
Andrew Ryan
  • 1,489
  • 3
  • 15
  • 21