O.Owoso
O.Owoso

Reputation: 1

Generating more random values within a very short range

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

Upvotes: 0

Views: 39

Answers (1)

Bob
Bob

Reputation: 236

Try this:

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

Upvotes: 1

Related Questions