Clarification on deprecation warning for seeding

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(datetime.time(s))

Hi i just started learning the language. What exactly is meant by above warning and what needs to be done to avoid such warning in future?

datetime.time(s) returns a time object. The only types supported by random.seed are the ones listed in the error message. The error message is telling you that you shouldn’t pass objects of unsupported types to random.seed, but rather pass something else.

I don’t know what s is in your program, or why you’re trying to seed the RNG like this.
Given that the datetime.time constructor looks like this

datetime.time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)

I believe there are only 24 possible values that you could be passing to random.seed here, which is almost certainly not what you wanted to do.

If you want to use the current time as a seed for the random number generator, use

random.seed(time.time())

instead.

1 Like

Thank you for your reply. I have modified my code.

The random module is automatically seeded when it is started up. You
don’t need to call seed unless you want to set it to a known value so
that you can reproduce the same random values each time.

import random
random.seed(1257)
[random.randint(1, 6) for i in range(10)]

which will return ten random numbers:

[5, 4, 2, 3, 2, 4, 5, 3, 6, 3]

Then, if you want the same numbers on the next run of your program,
you can set the seed to the same value you used before:

random.seed(1257)
[random.randint(1, 6) for i in range(10)]

which will reset the PRNG and give the same ten numbers. This is useful
for when you need to re-run a program and get the same results.

Otherwise, there’s not really any need to seed the random module. It’s
already seeded in a way that is more unpredictable than just using the
time.

Thank you Steven.