Cauchy random number generator

import math 
import random

def cauchy(location, scale):

    # Start with a uniform random sample from the open interval (0, 1).
    # But random() returns a sample from the half-open interval [0, 1).
    # In the unlikely event that random() returns 0, try again.
    
    p = 0.0
    while p == 0.0:
        p = random.random()
        
    return location + scale*math.tan(math.pi*(p - 0.5))

This code is in the public domain. Do whatever you want to with it, no strings attached.

See also:
Stand alone code
Python random number generation

 

Home