Allocating random numbers in 2D array


#1

def random_row(board):
return randint(0, len(board) - 1)

def random_col(board):
return randint(0, len(board) - 1)

How does it allocate random numbers to row and column. Like in C++ we provide index number. By calling in such a way it may the overwrite the same index. for example number stored at 2nd row 5th column by the first def(i.e def random_row) . There might be possibility that the same index is occupied by second def(i.e def random_col).


#2

you have a question?


#3

Okay I got that. It's a location not the value to be stored.


#4

no, it is just a number you generate:

randint(min,max)

the 2 arguments you provide are the minimum (0) and maximum(len(board)-1) which is 4. So now the random number you generated can be one of the following: 0,1,2,3,4. This way you get random coordinates for your ship

so now you have two random numbers, which are now your coordinates for you multi dimensional list


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.