PythonUserNew
PythonUserNew

Reputation: 91

Python randomnumber array

So, i have a small issue witch creating random numbers for an 2d array in python.

I have a matrix looking like this

matrix:
       row = ['.' for i in range(w)]
        self.matrix = [row]
        for j in range(h - 1):
            row = ['.' for i in range(w)]
            self.matrix.append(row)

w = int(input("please insert the width of the array: "))
h = int(input("please insert the height of the array: "))

user = int(input("please insert a number for objects: "))

x = random.sample(range(w), user)
y = random.sample(range(h), user)

list(zip(x, y))

So, now i want to create so many random numbers as user input. For example, it the user inputs 100, i need 100 numbers from x and y, so that i can use them as position, to insert objects randomly into the array.

But if the width and height are smaller then userinput i get an error.

Upvotes: 0

Views: 112

Answers (1)

dd23
dd23

Reputation: 381

Using the following two lines for x and yshould give you the results you expect:

x = [random.randint(0, w-1) for c in range(user)]
y = [random.randint(0, h-1) for c in range(user)]

Upvotes: 2

Related Questions