#NEWLINE# def sample_length( self , prob_end ) : #NEWLINE# #INDENT# '\n        Sample length of a game.\n\n        This is using inverse random sample on a probability density function\n        <https://en.wikipedia.org/wiki/Probability_density_function> given by:\n\n        f(n) = p_end * (1 - p_end) ^ (n - 1)\n\n        (So the probability of length n is given by f(n))\n\n        Which gives cumulative distribution function\n        <https://en.wikipedia.org/wiki/Cumulative_distribution_function>:\n\n        F(n) = 1 - (1 - p_end) ^ n\n\n        (So the probability of length less than or equal to n is given by F(n))\n\n        Which gives for given x = F(n) (ie the random sample) gives n:\n\n        n = ceil((ln(1-x)/ln(1-p_end)))\n\n        This approach of sampling from a distribution is called inverse\n        transform sampling\n        <https://en.wikipedia.org/wiki/Inverse_transform_sampling>.\n\n        Note that this corresponds to sampling at the end of every turn whether\n        or not the Match ends.\n        ' #NEWLINE# try : #NEWLINE# #INDENT# x = random . random ( ) #NEWLINE# return int ( ceil ( ( log ( ( 1 - x ) ) / log ( ( 1 - self . prob_end ) ) ) ) ) #NEWLINE# #UNINDENT# except ZeroDivisionError : #NEWLINE# #INDENT# return float ( 'inf' ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# return 1