samedi 30 décembre 2023

How can i pass a random number generator as argument to a function that will produce several different numbers inside the function? [duplicate]

So, I have created several different random number generator functions (rng) that produce values of a random variable X which follow different distribution functions. Now, I want to experiment with the central limit theorem, and have a function (x_n) that produces n different values of X for a given distribution function, so, given the distribution function, I want to produce several different values of X, and then find their mean value.

I tried to just define a function that takes as arguments the number of values I want to generate and the function f (the rng) that I want the variable X to follow. I tried to call the RNG inside the function several times, but, of course, when I put the predefined distribution function as an argument into the x_n(n,rng), it generates only one random number, into the argument section, and then this random number is used throughout the entire run of x_n.

How can I call the rng many times inside the function x_n(n,rng) and produce many different numbers inside of it?

import random as r

#def uniform():
    #return r.random()

def x2():
    u=r.random()
    return u**(1/3)

def x_n(n,f):
    x_n=0
    for i in range(n):
        rand=f
        x_n+=rand
        #print(rand)
    return x_n/n

x_n(5,x2())
x_n(7,r.random())
x_n(20,uniform())

P.S.: One idea I had was to create random number generators that take an argument and just change the argument inside x_n()'s loop, but I was hoping there was another way, so that I can pass just r.random() as the distribution function.




Aucun commentaire:

Enregistrer un commentaire