0

I will like to generate 50 random numbers between 1 and 4, I also expect to have floats but this isnt workingm any help will be appreciated.

randomlist = random.sample(range(1, 4), 50)
print(randomlist)

This is what I have been getting

> --------------------------------------------------------------------------- ValueError                                Traceback (most recent call
> last) <ipython-input-119-5bcf53f2047d> in <module>
> ----> 1 randomlist = random.sample(range(1, 4), 50)
>       2 print(randomlist)
> 
> C:\ProgramData\Anaconda3\lib\random.py in sample(self, population, k)
>     319         n = len(population)
>     320         if not 0 <= k <= n:
> --> 321             raise ValueError("Sample larger than population or is negative")
>     322         result = [None] * k
>     323         setsize = 21        # size of a small set minus size of an empty list
> 
> ValueError: Sample larger than population or is negative
  • Does this answer your question? [Generate random integers between 0 and 9](https://stackoverflow.com/questions/3996904/generate-random-integers-between-0-and-9) – vallentin Aug 22 '20 at 01:59
  • It does mention [in the docs](https://docs.python.org/3/library/random.html#random.sample): `If the sample size is larger than the population size, a ValueError is raised` You probable want `random.choices()` for discreet values or `random.uniform()` and friends for floats. – Mark Aug 22 '20 at 02:01
  • Try using numpy's `np.random.rand(50) * 3 + 1` – RichieV Aug 22 '20 at 02:11

1 Answers1

1

Try this:

import random
randomlist = [random.uniform(1,4) for i in range(50)]

Bob
  • 236
  • 1
  • 4