Reputation: 1356
I have integers in the range of 1..10. I want to choose one based on a distribution of x^2.
What I want is something like:
def random_from_exp(min, max, exponent):
...
I'm not familiar with maths or stats and I don't know how to interpret previous answers to this question (lambdas, inversions and whatnot) in the context of my problem above. Please provide a simple explanation with an example.
Upvotes: -1
Views: 1100
Reputation: 579
# https://miptstats.github.io/courses/python/07_random.html
import numpy as np
from numpy import random
import matplotlib.pyplot as plt
scale= 4
size= 1_000_000
x= np.random.exponential(scale, size)
plt.hist(x, bins=50, density=True)
plt.show()
a random number on an exponential distribution
do you really mean several same-parametrized distributions? I suppose, from that x_distr (with params given above) you can make sample from size
repeats in such a way:
from scipy.stats import rv_discrete
random_state = 1234
Iterations= 2000 # ? - probably, just size of newly generated sample! https://mipt-stats.gitlab.io/courses/python/07_scipy_stats.html
##x = np.random.randn(100_000)
unique, counts = np.unique(x, return_counts=True)
print(unique, counts)
# create object (use rv_continuous for continuous data)
r = rv_discrete(name='some_distribution', values= (unique, counts/counts.sum()) ) #, size= Iterations)
# make sample
sample = r.rvs(size= Iterations, random_state= 123456)
plt.hist(sample, bins= 20, density= True)
plt.show()
P.S. or single exp distr can be taken also from scipy.stats:
from scipy.stats import expon
lamb = 1
# The middle 80% of probability mass is located betweenthe 0.1 and 0.9 quantiles
rv = expon(scale = 1 / lamb)
q1 = rv.ppf(0.1)
q2 = rv.ppf(0.9)
Upvotes: 0
Reputation: 8463
If I'm right, you want to simply generate a number that is on the exponential function (x^2) and is between 0 to 10?
That's pretty simple. Get a random number from 0 to the square root of 10 and square it.
import random
import math
print random.uniform(0, math.sqrt(10))**2
Or in your case :
# I have no idea what the exp means. I'm assuming it means exponent.
def random_from_exp(min, max, exp):
return random.uniform(min, math.sqrt(max))**exp
This will print (min <= RESULT <= max) ^ exp
Upvotes: 1