Answer the question
In order to leave comments, you need to log in
What is the probability of getting identical sequences?
There are two options to get a sequence of numbers
. The first one is the usual shuffle
def get_seq1(N=10):
from random import shuffle
seq1 = [X for X in range(N)]
shuffle(seq1)
return seq1
def get_seq2(N=10):
from random import shuffle
from random import randrange
seq2 = []
numbers = [X for X in range(N)]
for _ in range(N):
shuffle(numbers)
seq2.append(numbers.pop(randrange(0, len(numbers))))
return seq2
seq11 = get_seq1()
seq12 = get_seq1()
print(seq11 == seq12)
seq21 = get_seq2()
seq22 = get_seq2()
print(seq21 == seq22)
Answer the question
In order to leave comments, you need to log in
get_seq2 does nothing useful if you don't need some intermediate results. The distributions of get_seq1 and get_seq2 are the same. Only get_seq2 is 7 times slower.
Obviously, since the distributions are the same, then the probability of getting the same sequence is the same as the probability of getting it inside the same function - P(get_seq1() == get_seq1()) == P(get_seq1() == get_seq2()) == 1 / N * 1 / N-1 * ... * 1/1, where N is the length of the sequence
The distribution is very easy to measure:
def measure(fn):
n = 10
p = []
for _ in range(10):
p.append([0] * n)
for _ in range(10**6):
seq = fn(n)
for i, el in enumerate(seq):
p[el][i] += 1
return p
>>> measure(get_seq1)
>>> measure(get_seq2)
Well, if you need to prevent this from happening, you can limit it. First "roll" one function, then another with the condition that the result is not equal to the result of the previous function.
Well, in general, this is all limited to the theory of probability. The chance that you will get one number out of 10 is 1/10, respectively. The chance that you will get the same number out of 10 twice is 1/10*10 = 0.01.
You need to understand that the chance of falling out of any element of the list is the same. Those. the chance that 5 will fall out is the same as the chance that 6 will fall out. Accordingly, to get the probability of the sequence, all this must be multiplied, I don’t remember the formula, to be honest, look it up on the Internet. Well, I'll try to think logically and just raise it all to the 10th power. those. 0.01^10 would be about 1.0E-20
Herethere is a good article on the theory of probability and randomness. Examples on dice, but I think it will fit you, I advise you to read at your leisure, an interesting thing.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question