userrandomnums
userrandomnums

Reputation: 255

Flipping random element in matrix

I have a matrix (in list form) and I'm trying to do two things.

First thing is to select a random element in the matrix and switch it from 1 to 0 or 0 to 1.

Second thing is to do the same thing, but for more than one element; I need to solve the first problem first!

I have the switch function (to flip the zeroes and ones) and the next line is trying to switch a random element in the matrix (matrx) between 0 and the size of the matrix minus one (or else it would be out of range).

I guess the problem is the int(size-1) part. Just using size works, but I need it to be size-1.

The "Size" variable isn't 0.

def switch(e):
    return ''.join('1' if x == '0' else '0' for x in str(e))

switch(matrx[randint(0,(size-1))][randint(0,(size-1))])

ValueError: empty range for randrange() (0,0, 0)

Upvotes: 1

Views: 164

Answers (1)

Petar Ivanov
Petar Ivanov

Reputation: 93020

Looks like your size has a value of 0. The error is from the randint function...

Upvotes: 2

Related Questions