1

I know that I can create an array of unique random numbers using Random.Sample within a defined range of numbers.

What I need to do is basically create a 2D array of 5 rows and 5 columns and each index will contain a pair of two numbers. However, all the 25 pairs must be unique in the whole 2D Array, and the range of numbers is 0-4 (five numbers in total, so total possible pairs are also 5x5 which is 25)

That is, one possible 2D array can be,

Row 1 -> [[0,1], [0,2], [0,3], [1,0], [2,0]]
Row 2 -> [[0,4], [1,1], [1,2], [2,1], [3,0]]
Row 3 -> [[1,3], [1,4], [4,0], [4,1], [3,1]]
Row 4 -> [[2,2], [2,4], [2,3], [4,2], [3,2]]
Row 5 -> [[4,3], [3,3], [4,4], [3,4], [0,0]]

I have tried various ways to do this but I couldn't achieve the required results. How can I do this using Random.Sample function?

2
  • Why do you have to use random.sample()? Commented Feb 22, 2022 at 15:54
  • @Mr.T not necessarily has to be random.sample(). Only mentioned it because it is what I have seen being used by everyone (on stackoverflow and other places) for generating unique random numbers in a given range. My pairs have to be unique throughout the 2D array, so I thought random.sample() might be the way to go Commented Feb 22, 2022 at 15:57

2 Answers 2

2

Base Python solution:

from random import shuffle

n = 5
#create all combinations of index values
l = [[i, j]  for i in range(n) for j in range(n)]
#shuffle the list
shuffle(l)
#subdivide into chunks
res = [l[i:i+n] for i in range(0, n**2, n)]
print(res)

Sample output:

[[[0, 2], [3, 3], [4, 3], [4, 0], [3, 4]], [[1, 0], [0, 4], [0, 1], [4, 4], [2, 1]], [[0, 0], [2, 3], [2, 2], [2, 0], [1, 1]], [[1, 2], [3, 0], [4, 1], [3, 1], [2, 4]], [[0, 3], [1, 4], [1, 3], [3, 2], [4, 2]]]
Sign up to request clarification or add additional context in comments.

2 Comments

That worked like a charm, thank you!!! But can you please explain what does the l[i:i+n] do? I understand everything else, except this bit. Would be highly thankful to you.
List slicing l[start:stop:step] takes from start to stop every stepth element. If step is not specified, the default is 1. In this code, we divide the list of list into chunks [0:n-1], [n:2n-1], [2n:3n-1],... because range(0, n**2, n) provides i as 0, n, 2n...
1

Another alternative that uses a set to generate the desired number of unique pairs.

from random import randrange

pairs = set()
while len(pairs) < 25:
    pair = randrange(5), randrange(5)
    pairs.add(pair)

pairs = list(pairs)
array = [pairs[i: i+5] for i in range(0, 25, 5)]

for i, row in enumerate(array, start=1):
    print(f'row {i} -> {row}')

Result:

row 1 -> [(0, 0), (3, 1), (1, 1), (0, 2), (3, 3)]
row 2 -> [(1, 3), (2, 4), (0, 4), (1, 0), (4, 0)]
row 3 -> [(2, 0), (1, 2), (4, 2), (3, 4), (2, 2)]
row 4 -> [(1, 4), (4, 4), (3, 0), (4, 1), (2, 1)]
row 5 -> [(0, 1), (3, 2), (4, 3), (2, 3), (0, 3)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.