-1

The below function collects random numbers but the seed parameter should be optional. Therefore, I added the * .

import random

def random_num(n, number, *seed): #n is the number of values to return and number is the range of the uniform distribution that uses later
    collect = []
    random.seed(seed)
    for i in range(n):
        collect.append(round(random.uniform(-number, number),2))    
    return collect

Running the function without using the seed parameter:

random_num(4,5)

The result is: [4.82, -3.12, -0.62, 0.27] which looks fine I guess. It also shows the warning below:

DeprecationWarning: Seeding based on hashing is deprecated since Python 3.9 and will be removed in a subsequent version. The only supported seed types are: None, int, float, str, bytes, and bytearray. random.seed(seed)

What is the proper way to simply make the seed parameter optional without issues?

saZ
  • 89
  • 7

1 Answers1

1

You could just default the seed to 'None'

def random_num(n, number, seed=None): 
    random.seed(seed)
    [...]

This then uses the default seed (current system time) https://docs.python.org/3/library/random.html

JeffUK
  • 4,107
  • 2
  • 20
  • 34
  • Interesting. What do you think about setting `seed = False`. When would be used? – saZ Nov 28 '21 at 12:47
  • It depends what behaviour you want. `None` will use the default source of randomness for the seed, `False` will set the seed to 0 (so you will always get the same results.) – JeffUK Nov 28 '21 at 12:50
  • any info on what the warning was about? – saZ Nov 28 '21 at 12:59
  • 3
    If you use *seed , then seed is always a tuple, it may be an empty tuple. random.seed no longer accepts tuples as an argument – JeffUK Nov 28 '21 at 13:02