How would I generate random numbers to 2 decimal places in the range (0, 0.1]. My code so far is generating numbers with two decimal places in the range [0, 0.1]:
radius = round(random.uniform(0, 0.1), 2) How would I generate random numbers to 2 decimal places in the range (0, 0.1]. My code so far is generating numbers with two decimal places in the range [0, 0.1]:
radius = round(random.uniform(0, 0.1), 2) Perhaps the easiest way to solve your problem is to think in terms of integers, not floating-point numbers.
You basically want possible random numbers like 0.01, 0.02, 0.03, ..., 0.09, 0.10.
First you generate an integer between 1 to 10 inclusive, then you divide by 100.0 to get a floating-point number.
Here is the code:
x = random.randint(1, 10) y = x / 100.0 Documentation: random.randint(a, b)
radius = round(random.uniform(0.005, 0.10499999999999), 2) print(radius) you also need to extend the range of the sampled values to make sure that the probability of selecting the edge numbers 0.01 and 0.1 is the same as that of any other number in the desired range. otherwise, they will be sampled with half of probability of other numbers. I assume that round function rounds up i.e. 0.105 would end up as 0.11 not 0.10.
Try subtract random value from max value as uniform includes low, but excludes high then you will get values from 0 to max excluding low and including high:
radius = round(0.1-random.uniform(0, 0.1), 2)