-4

I want to generate a set of 300 random numbers between 0 and 0.01 , the random numbers should includes 0 and 0.01 as well

Mahesh
  • 13
  • 5
  • `a = np.random.uniform(size=300)`, then scale `a` to `0.01`, e.g. `a /= a.max() * 100` – Quang Hoang Aug 30 '22 at 18:06
  • what did you try ? you tagged the question with numpy and random, didn't found a suitable function there ? – Rabinzel Aug 30 '22 at 18:06
  • [this page](https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.random.html) gives specific instructions: `(b - a) * random() + a`. Note that it doesn't matter if the random number generator includes the left and right points or not - because the probability of generating them goes to 0 anyways – Vladimir Fokow Aug 30 '22 at 18:08
  • i want my array to have both 0 and 0.01 as a part of random numbers, please help me in this – Mahesh Aug 30 '22 at 18:16
  • there are infinitely many numbers to choose from -> probability of choosing exactly 0 or exactly 0.01 is `0` -> they will never be chosen – Vladimir Fokow Aug 30 '22 at 18:19
  • @QuangHoang i want my random numbers to have both 0 and 0.01 – Mahesh Aug 30 '22 at 18:19
  • @VladimirFokow there will be a way, my random numbers can be as this following array [0, 0.01,0.002,.........0,0.001] – Mahesh Aug 30 '22 at 18:21
  • Does this answer your question? [Generate random number between 0.1 and 1.0. Python](https://stackoverflow.com/questions/16288749/generate-random-number-between-0-1-and-1-0-python) – Vladimir Fokow Aug 30 '22 at 18:22
  • @VladimirFokow no sir, i have tried that method but it is giving different output. I want 0 to be in my array – Mahesh Aug 30 '22 at 18:26
  • Would you like to round your numbers as in the first answer there? – Vladimir Fokow Aug 30 '22 at 18:27
  • @VladimirFokow sir, it has got nothing to do with number of decimal places, i need to have my end points also part of the random numbers – Mahesh Aug 30 '22 at 18:33
  • @VladimirFokow there are much less than "infinitely many numbers to choose from", specifically only `2**53` uniform values in [0,1) can ever be returned using a `float`. hence the probability of seeing an endpoint is `2**-52`. – Sam Mason Aug 31 '22 at 10:21
  • @SamMason agree. With only 300 numbers though, it will _practically_ never occur. The OP's intent is not clear - should the range of random numbers to select from include the endpoints, of the resulting number list should always have them – Vladimir Fokow Aug 31 '22 at 10:31

1 Answers1

0
from numpy.random import default_rng
rng = default_rng()

With rounding:

n = 300
a = 0
b = 0.01
dec_points = 10

exclusive = (b - a) * rng.random(n) + a      # not including b
inclusive = np.round(exclusive, dec_points)  # including b

You can check that inclusive really includes b if you set dec_points to 3, for example - then the probability of having 0.01 will increase and you might be able to spot them in the generated numbers. But as dec_points increases - you will see that 0.01 is being generated less and less often.


Without rounding:

n = 300
a = 0
b = 0.01

epsilon = 1e-10
result = []
while len(result) < n:
    num = (b + epsilon - a) * rng.random() + a  # can be slightly higher than b
    if num <= b:
        result.append(num)
result = np.array(result)

Note:

With this approach the probability of getting exactly 0 or exactly 0.01 is so low, that you probably will never see them.

Vladimir Fokow
  • 3,728
  • 2
  • 5
  • 27
  • the output is not including 0, i need to have 0 in my random numbers. Just need a set of random numbers between 0 and 0.01(including both the numbers) – Mahesh Aug 31 '22 at 05:27
  • @Mahesh, the output _does_ include 0. I just checked with : `dec_points = 3`, and got these numbers as a part of `inclusive` array: `0. , 0.002, 0.003, 0.003, ... ` – Vladimir Fokow Aug 31 '22 at 08:05
  • You understand that since these numbers are random, you can't **guarantee** that 0 will be in every output, right? If you need a guarantee, maybe [`.choice`](https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.choice.html?highlight=choice#numpy.random.Generator.choice) or [`.shuffle`](https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.shuffle.html) would better suit your needs? – Vladimir Fokow Aug 31 '22 at 08:06
  • @Mahesh added a second method – Vladimir Fokow Aug 31 '22 at 08:25
  • why not just generate `n-2` numbers, then add in `a` and `b` at the end. – Sam Mason Aug 31 '22 at 10:14