#!/usr/bin/env python
# pip install numpy scipy matplotlib pyperclip
import numpy as np
import scipy.stats
import matplotlib.pyplot as plt
import pyperclip

def my_distribution(min_val, max_val, mean, std):
    scale = max_val - min_val
    location = min_val
    # Mean and standard deviation of the unscaled beta distribution
    unscaled_mean = (mean - min_val) / scale
    unscaled_var = (std / scale) ** 2
    # Computation of alpha and beta can be derived from mean and variance formulas
    t = unscaled_mean / (1 - unscaled_mean)
    beta = ((t / unscaled_var) - (t * t) - (2 * t) - 1) / ((t * t * t) + (3 * t * t) + (3 * t) + 1)
    alpha = beta * t
    # Not all parameters may produce a valid distribution
    if alpha <= 0 or beta <= 0:
        raise ValueError('Cannot create distribution for the given parameters.')
    # Make scaled beta distribution with computed parameters
    return scipy.stats.beta(alpha, beta, scale=scale, loc=location)

np.random.seed(100)

min_val = 18   # Minimum value
max_val = 60    # Maximum value
mean = 28.32     # Average number
std = 6.38       # Standard deviation
my_dist = my_distribution(min_val, max_val, mean, std)
# Plot distribution PDF
x = np.linspace(min_val, max_val, 100)
plt.plot(x, my_dist.pdf(x))
#np.set_printoptions(precision=2)   # Two decimal places
np.set_printoptions(formatter={'float': '{: 0.2f}'.format})   # Two decimal places
# Stats
print('mean:', my_dist.mean(), 'std:', my_dist.std())
# Get a large sample to check bounds
sample = my_dist.rvs(size=60)      # Generation quantity
print('min:', sample.min(), 'max:', sample.max())
print(sample)

# Copy to clipboard
text = str()
for i in sample:
    text += str(('%.2f'%i)) + "\n"   # Two decimal places
#text = text[:text.rfind('\n')]
text = text[:-1]
pyperclip.copy(text)
