Reputation: 33
Is it possible to have more than one variables and each have a unique and distinct random number from one another. The sample range is the same, yet I do not want the variables to have same numbers.
import random
A=random.sample(range(1,4),1)
B=random.sample(range(1,4),1)
C=random.sample(range(1,4),1)
So the above example, I want A, B and C to have different random numbers. In other words, no two variables must have the same number.
How to go about this?
Upvotes: 0
Views: 47
Reputation: 672
This should work:
A, B, C = random.sample(range(1, 4), k=3)
As far as I know, does random.choice
not prevent from having all three Variables containing the same value, while sample
does assign each value given in the range
only once each call
Upvotes: 2
Reputation: 2453
You can do
y = range(1, 4)
x = np.random.choice(y, 3, replace=False)
print(x)
[2 1 3]
Upvotes: 1