Reputation: 41
I need to choose a random float number in two ranges in python:
0. < n < 0.2 or 0.8 < n < 1.
Right now I only have one range:
random.uniform(0, 0.2)
The full line (I'm mapping warm color hsv values):
couleur = hsv2rgb(random.uniform(0, 0.2), 1, 1))
If someone could help... !
Upvotes: 4
Views: 1215
Reputation: 12391
Create a random number in 0 < n < 0.4
and map the upper half of that interval to your desired range.
nbr = random.uniform(0, 0.4)
if nbr >= 0.2:
nbr += 0.6
Upvotes: 3
Reputation: 5031
You can do a weighted selection between the intervals:
from numpy import random
def uniform_two(a1, a2, b1, b2):
# Calc weight for each range
delta_a = a2 - a1
delta_b = b2 - b1
if random.rand() < delta_a / (delta_a + delta_b):
return random.uniform(a1, a2)
else:
return random.uniform(b1, b2)
print uniform_two(0, 0.2, 0.8, 1)
Upvotes: 4