How many unique passwords can be made from 6 digits (0 to 9) if repeats are possible?

For those of you who enjoy functional python:

from itertools import imap, starmap, islice, repeat
from functools import partial
from string import letters, digits, join
from random import choice

join_chars = partial(join, sep='')
identity = lambda o: o

def irand_seqs(symbols=join_chars((letters, digits)), length=6, join=join_chars, select=choice, breakup=islice):
    """ Generates an indefinite sequence of joined random symbols each of a specific length
    :param symbols: symbols to select,
        [defaults to string.letters + string.digits, digits 0 - 9, lower and upper case English letters.]
    :param length: the length of each sequence,
        [defaults to 6]
    :param join: method used to join selected symbol, 
        [defaults to ''.join generating a string.]
    :param select: method used to select a random element from the giving population. 
        [defaults to random.choice, which selects a single element randomly]
    :return: indefinite iterator generating random sequences of giving [:param length]
    >>> from tools import irand_seqs
    >>> strings = irand_seqs()
    >>> a = next(strings)
    >>> assert isinstance(a, (str, unicode))
    >>> assert len(a) == 6
    >>> assert next(strings) != next(strings)
    """
    return imap(join, starmap(breakup, repeat((imap(select, repeat(symbols)), None, length))))

It generates an indefinite [infinite] iterator, of joined random sequences, by first generating an indefinite sequence of randomly selected symbol from the giving pool, then breaking this sequence into length parts which is then joined, it should work with any sequence that supports getitem, by default it simply generates a random sequence of alpha numeric letters, though you can easily modify to generate other things:

for example to generate random tuples of digits:

>>> irand_tuples = irand_seqs(xrange(10), join=tuple)
>>> next(irand_tuples)
(0, 5, 5, 7, 2, 8)
>>> next(irand_tuples)
(3, 2, 2, 0, 3, 1)

if you don't want to use next for generation you can simply make it callable:

>>> irand_tuples = irand_seqs(xrange(10), join=tuple)
>>> make_rand_tuples = partial(next, irand_tuples) 
>>> make_rand_tuples()
(1, 6, 2, 8, 1, 9)

if you want to generate the sequence on the fly simply set join to identity.

>>> irand_tuples = irand_seqs(xrange(10), join=identity)
>>> selections = next(irand_tuples)
>>> next(selections)
8
>>> list(selections)
[6, 3, 8, 2, 2]

As others have mentioned if you need more security then set the appropriate select function:

>>> from random import SystemRandom
>>> rand_strs = irand_seqs(select=SystemRandom().choice)
'QsaDxQ'

the default selector is choice which may select the same symbol multiple times for each chunk, if instead you'd want the same member selected at most once for each chunk then, one possible usage:

>>> from random import sample
>>> irand_samples = irand_seqs(xrange(10), length=1, join=next, select=lambda pool: sample(pool, 6))
>>> next(irand_samples)
[0, 9, 2, 3, 1, 6]

we use sample as our selector, to do the complete selection, so the chunks are actually length 1, and to join we simply call next which fetches the next completely generated chunk, granted this example seems a bit cumbersome and it is ...

Learning Outcomes

  • Compute a conditional probability for an event
  • Use Baye’s theorem to compute a conditional probability
  • Calculate the expected value of an event

We can use permutations and combinations to help us answer more complex probability questions.

examples

A 4 digit PIN number is selected. What is the probability that there are no repeated digits?

Try It

Example

In a certain state’s lottery, 48 balls numbered 1 through 48 are placed in a machine and six of them are drawn at random. If the six numbers drawn match the numbers that a player had chosen, the player wins $1,000,000.    In this lottery, the order the numbers are drawn in doesn’t matter. Compute the probability that you win the million-dollar prize if you purchase a single lottery ticket.

Example

In the state lottery from the previous example, if five of the six numbers drawn match the numbers that a player has chosen, the player wins a second prize of $1,000. Compute the probability that you win the second prize if you purchase a single lottery ticket.

The previous examples are worked in the following video.

examples

Compute the probability of randomly drawing five cards from a deck and getting exactly one Ace.

Example

Compute the probability of randomly drawing five cards from a deck and getting exactly two Aces.

View the following for further demonstration of these examples.

Try It

Birthday Problem

Let’s take a pause to consider a famous problem in probability theory:

Suppose you have a room full of 30 people. What is the probability that there is at least one shared birthday?

Take a guess at the answer to the above problem. Was your guess fairly low, like around 10%? That seems to be the intuitive answer (30/365, perhaps?). Let’s see if we should listen to our intuition. Let’s start with a simpler problem, however.

example

Suppose three people are in a room.  What is the probability that there is at least one shared birthday among these three people?


Suppose five people are in a room.  What is the probability that there is at least one shared birthday among these five people?


Suppose 30 people are in a room.  What is the probability that there is at least one shared birthday among these 30 people?

The birthday problem is examined in detail in the following.

If you like to bet, and if you can convince 30 people to reveal their birthdays, you might be able to win some money by betting a friend that there will be at least two people with the same birthday in the room anytime you are in a room of 30 or more people. (Of course, you would need to make sure your friend hasn’t studied probability!) You wouldn’t be guaranteed to win, but you should win more than half the time.

This is one of many results in probability theory that is counterintuitive; that is, it goes against our gut instincts.

Try It

Suppose 10 people are in a room. What is the probability that there is at least one shared birthday among these 10 people?

How many 6

So, starting from the left, you would have 9 choices for the leftmost digit (not allowing 0), then 9 choices again for the next digit (0 is now allowable but not the previous digit), then 8 choices for the next digit and so on, for a result of 9*9*8*7*6*5 = 136080.

How many combinations are there for a 6

"Mathematically speaking, there is a huge difference, of course," says Philipp Markert. A four-digit PIN can be used to create 10,000 different combinations, while a six-digit PIN can be used to create one million.

How many unique numbers are there in 6 digits?

∴ In total, there are 900,000 6-digit numbers.

How many six digit passwords are possible if repetition of digits is allowed?

This is simply 999999 - 0 + 1 = 1000000 (1 million) possible passwords.