Search This Blog

Thursday, April 9, 2020

About "random" numbers

It is an interesting thing that if you were to ask some person to give you a "random" 6-digit number, they would tend to try really hard to not repeat digits.  People have a tendency to think that random number do not have repeating digits.  But an analysis of these numbers shows that, in fact, for longer numbers, it starts to be more likely that there are repeated digits than not.  Below is a table of the number of digits and the likelihood that there are repeating digits.
(argh, excuse me but there is not a control to create a table!!)
Number of digits vs. likelihood of repeated digits:

  1. None
  2. 9.99%
  3. 28%
  4. 50%
  5. 70%
  6. 85%
  7. 94%
  8. 98%
  9. 99%
  10. nearly 100%
So the psychology going on here is that people have a sense in 2 digit and maybe 3 digit numbers that repeated digits in a number are rarer than unique.  However, in reality, when you get to 5 and 6 digit numbers, most numbers have repeated digits.
The math of permutations and combinations is explained here .
The python math module functions are explained here.  The ones that are relevant are the factorial, comb and perm functions.  I wrote some code to re-create these.  I found out that after 10 levels of recursion, Python gives you an error message.  So I had to use the math module version of the factorial function.

# This defines the permutation function
# and the combination function

from math import factorial

def perm(n,r):
    return factorial(n)/factorial(n-r)

def comb(n,r):
    return factorial(n)/factorial(n-r)/factorial(r)

for i in range(2,11):
    print(i,1-perm(10,i)/10**i)

Also, if you don't believe the math, I also wrote a piece of code that does a brute force calculation.  It does take some time to work its way through all the different combinations.  And it will not give the exact same results since it is not considering the numbers that start with 0.
 
# This program prints out how many of the numbers in a range
# have repeating digits

n = 2
flag = True
while flag:
    test = 10**(n-1)
    count = 0
    print(test)
    while test < 10**n:
        a = str(test)
        dupe = False
        for i in range(10):
            if a.count(str(i))>1:
                dupe = True
        if dupe:
            count = count + 1
        test = test +1
    ratio = count / 10**n
    print(n,count, ratio)
    ask = input("continue? (n to end) >")
    if ask == 'n':
        flag = False
    n = n+1
So it's an interesting bit of psychology so I hope you enjoy it.             

No comments:

Post a Comment