1
$\begingroup$

I use the following code snippet to generate the random characters

char_set = string.ascii_letters + string.digits
Randomno = ''.join(random.sample(char_set*10,9))

Here, I multiply the number of symbols by $10$ so we get $620 ^ 9 = 13537086546263552000000000$ before a number repeats. But will random.sample actually generate all these possible combinations?

$\endgroup$

1 Answer 1

1
$\begingroup$

As far as i know the samples are generated independently. That would be very inefficient. So random.sample draws a number in the interval $[0, 620]$ and moves on to the next index.

Because the probabilities of drawing a certain character are independent the probability of repeating a character, i.e. $..., 10, 10, ...$ is $\frac{1}{620} \frac{1}{620}$, which is very low. The chance of generating two consecutive samples that are equal, e.g. $1, 2, 3, 4, 5, 6, 7, 8, 9$ two times is $\frac{1}{620^9} \frac{1}{620^9}$ which is even lower.

So there are $620^9$ combinations which means that on average the same combinations occurs after $620^9$ trials.

However, the chance is very low but it could happen faster. As i said, its on average, because it's a random process.

$\endgroup$
2
  • $\begingroup$ Randomno returns a string of characters which is 9 characters long. So I was interested in knowing the probability of the entire sequence being repeated. Say in the first run I get 123abc456. After how many runs will this sequence repeat? @hh32 $\endgroup$
    – Shre1407
    Commented Aug 31, 2017 at 13:57
  • $\begingroup$ I know that. But what i meant is that each part of that is generated independently, i.e. 1, then 2, then 3, and so on. I edited my answer $\endgroup$
    – hh32
    Commented Aug 31, 2017 at 14:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.