How to draw a draw for an element of an array within a specific range?

Asked

Viewed 144 times

0

I have a Python code, where I have an array (g) of 10,000 elements. After performing a draw using Andom.Choice(g) that draws any of these 10000 elements. However, I would like my final value to be between a certain range, because eventually the draw ends up picking values out of the range.

First I put if and Else codition, but eventually it happens to draw values out of the range, because these coditions only prevent it from happening the first time. I thought about putting a while loop, but I can’t implement it. Below is part of the code

Basically, this is where I Gero my array with 10000 elements

mu = 0.7416
sigma = 0.0876
g = np.random.normal (mu, sigma, 10000)

Here is only the average value of the generated distribution, nothing that influences my doubt

def calculo_dist_internuclear():
    media = stats.gmean(g, axis=0)
    return media
print ('Distância internuclear mais provável:', calculo_dist_internuclear()

Here I draw the generated array

valor_escolhido = random.choice(g)
print ('O valor sorteado foi:', valor_escolhido)

if (0.5916 < valor_escolhido < 0.8916):
    valor_escolhido_FC1 = valor_escolhido
    print ("Sorteio de primeira:", valor_escolhido_FC1)

else:
    valor_escolhido_FC2 = random.choice(g)
    print ("Sorteio realizado novamente:", valor_escolhido_FC2)

1 answer

0

Hello alive beyond Random there are more options.... randrange and Uniform for integers and for floats.

Your example with both, adjust to what you need.

from random import randrange, uniform

# randrange returns one integral value
valor_escolhido = randrange(0, 10000)
print ('O valor sorteado foi:', valor_escolhido)

if (0.5916 < valor_escolhido < 0.8916):
    valor_escolhido_FC1 = valor_escolhido
    print ("Sorteio de primeira:", valor_escolhido_FC1)

else:
    # uniform returns floating-point value
    valor_escolhido_FC2 = uniform(0, 10000)
    print ("Sorteio realizado novamente:", valor_escolhido_FC2)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.