-1

Is there any way to make sure that my 2 randomly generated items will not be the same? For my current code, sometimes number1 will be the same as number2.

import random

car_list = [['Toyota', 8], ['Merc', 8], ['BMW', 8], ['Porshe', 8], ['RR', 8]]
number1 = random.choice(car_list)
number2 = random.choice(car_list)

print(number1)
print(number2)
ErdoganOnal
  • 800
  • 6
  • 13
  • Does this answer your question? [Generate 'n' unique random numbers within a range](https://stackoverflow.com/questions/22842289/generate-n-unique-random-numbers-within-a-range) – Tomerikoo Aug 01 '21 at 13:37

2 Answers2

2

You may use random.sample.

option1, option2 = random.sample(car_list, 2)
ErdoganOnal
  • 800
  • 6
  • 13
0

you can use this :

#Syntax : numpy.random.choice(a, size=None, replace=True, p=None)

import numpy as np

array = np.array([['Toyota', 8], ['Merc', 8], ['BMW', 8], ['Porshe', 8], ['RR', 8]])
# print("Printing 2D Array")
# print(array)

print("Choose 2 sample rows from 2D array")
randomRows = np.random.choice(2, size=2,replace=False)
for i in randomRows:
    print(array[i, :])

Output:

Choose 2 sample rows from 2D array
['Toyota' '8']
['Merc' '8']
Salio
  • 1,058
  • 10
  • 21