bell_curve_data.py

#!/usr/bin/python3
# ===================================================================
# write bell curve (normal distribution) data to a file
# ===================================================================
# the output data can be used for testing when working with a
# normal distribution
# ==================================================================

import user_interface as ui
import numpy as np
import re
import sys

# ---- global variables

mean   =    0.0    # bell curve data arithmetic mean (x value)
sd     =   50.0    # bell curve data standard deviation
xstart = -150.0    # bell curve x start value
xend   =  150.0    # bell curve x end value
xincr  =   10.0    # bell curve x increment value
ymax   =  200.0    # bell curve y maximum value (at peak; at mean)

## some other data to try - asymmetrical plot
##xstart = 100.0
##xend   = 300.0


# -------------------------------------------------------------------
# ---- return the bell curve y value for a given x value
# ---- x     plot x value
# ---- ymax  plot maximum plot y value (value at mean)
# ---- mean  bell curve data arithmetic mean    (x value)
# ---- sd    bell curve data standard deviation (x value)
# -------------------------------------------------------------------

def BellCurveValue(x,ymax,mean,sd):

    y = ymax * pow(np.e,-pow(x-mean,2.0)/(2.0*sd*sd))

    return y


# -------------------------------------------------------------------
# ---- main
# -------------------------------------------------------------------

if __name__ == '__main__':

    # ---- running Python3

    if not ui.running_python3():
        print()
        print('not running Python3 - exit program')
        print()
        sys.quit()

    # ---- open output file

    filename = 'bell_curve_data.dat'

    out = open(filename,'w')

    # ---- output

    ocount = 0                 # output count

    x = xstart

    while x <= xend:

        y = BellCurveValue(x,ymax,mean,sd)

       ##print(f'y = {y}')

        y = round(y)           # make Y an integer

        if y > 0:
            i = 0
            while i < y:
                out.write(f'{round(x)}\n')
                ocount += 1
                i += 1

        x += xincr

    # ---- close output file

    out.close()

    # ---- display stats

    print()
    print(f'{ocount} values written to output file ({filename})')
    print()