a_neolate
a_neolate

Reputation: 11

Making variables not equal to each other

I'm currently making a code whose function is to make a 2D array filled with a function which calculates multiples of a value x in range (1,10).

def find_multiples (twolist, count) : 

    n = random.randrange(1,10)

    for i in range(count) :
        for j in range(count) :
            x = random.randrange(0,101,n)
            twolist[i][j] = x

The next part is inputting randomly a value "y" which is different from x in a random place within the array.

y = random.randrange(0,101)
a = random.randrange(count)
b = random.randrange(count)
twolist[a][b] = y
return twolist, y

My question is, how do I make y a different value from x? I was thinking maybe I have to make a separate defined function which acts to check if y is not a multiple of n but I'm not quite sure how to word it.

def check_diffmult (numx , numy) :

    for i in range(1,numx) :
        if numy = random.randrange(0,101,i) :

This was my attempt so far, but I know it's off.

Edit : Thanks for the replies so far! So far, there isn't much else to the code except for this. The overall goal is for there to be a random integer y that is is a different pattern from the multiples of n which go through the function. Not sure if that's enough to clear things up! Also, I read a reply further down that y is as easy as x + 1, but I'm hesitant in changing a value of y by simply adding or subtracting since y could then accidentally become one of the multiples of the randomly chosen value of "n".

Edit 2 :

def check_diffmult (numx , numy) :
check_list = []
check_list.append(numx)
if numy == check_list :
    return 0
return 1

def find_multiples (twolist, count) :

n = random.randrange(1,10)
for i in range(count) :
    for j in range(count) :
        x = random.randrange(0,101,n)
        twolist[i][j] = x

while True :

    y = random.randrange(0,101)
    check = check_list(x,y)
    if check == 1 :
        a = random.randrange(count)
        b = random.randrange(count)

        twolist[a][b] = y
        break

return twolist, y

Changed code to this to make y != x . Could this be a viable code?

Upvotes: 1

Views: 245

Answers (1)

lynn
lynn

Reputation: 10814

How about something like:

x, y = random.sample(range(0, 101, n), 2)

Quoting the docs:

random.sample(population, k)

Return a k length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement. [...]

To choose a sample from a range of integers, use an range() object as an argument.

(If you're using Python 2, you might want to use xrange() instead.)

Upvotes: 2

Related Questions