dimanche 22 novembre 2020

I want to multiply random number between 1 and 2 and plot the results in a histogram

import numpy as np
import matplotlib.pyplot as plt
import random


Ntrials=50
x=random.randint(1,2)
mean=10
variance=20
mu=np.log(mean**2/np.sqrt(variance+mean**2))
sigma=np.sqrt(np.log((variance/mean**2)+1))

def LogNormal ():
    return (1/(x*np.sqrt(2*np.pi*sigma**2)) * np.exp(-(np.log(x)-mu)**2)/2*sigma**2)

def ToPlot(Ntrials):
    
    resultProduct=1
    resultTrials=[]
    for i in range(1,Ntrials):
        for j in range (x): 
            resultProduct= resultProduct * i
            resultTrials.append(resultProduct) 
   
    plt.hist(resultTrials,bins=20,density=True,)
    plt.show()

    plt.plot(x,LogNormal())
    plt.show()

ToPlot(50); ax=plt.gca(); ax.set_xlim(-1.5,10)

I want to generate random numbers between 1 and 2 and multiply them with each other and then plot a histogram of the results. Please help me fix this code so it can work.

This is the question : Find the product of N uniformly distributed numbers between 1 and 2 storing the result each time. Make a histogram of these N trial numbers. Calculate the mean and variance of the distribution




Aucun commentaire:

Enregistrer un commentaire