Python generates a couple of different random numbers

I want to use

np.random.randint(4, size=2)

to create a pair of different random numbers from 0 to 3. The problem is that it sometimes gives (0,0), (3,3), etc. Is there a way to make numbers be different?

+4
source share
2 answers

you can use random.choice:

import numpy as np

np.random.choice(a=np.arange(4), size=2, replace=False)

or, more concise (as Nuageux noted ):

np.random.choice(a=4, size=2, replace=False)
+7
source

for people who do not want to use numpy

import random


left_end = 0
right_end = 3
length = 2
random.sample(range(left_end, right_end), k=length)

you can also wrap it in tupleif you need to, as it random.samplereturns listan object

+1
source

Source: https://habr.com/ru/post/1677153/


All Articles