random in MicroPython does appear to do anything?

461 Views Asked by At

Using pybricks-micropython

Running this command to get a random port number, works under CPython.

import random
port = random.randint(50000,50999)

produces a number, only it is hardly random it is the same number each time I run the script. I am guessing MicroPython needs something more perhaps?

What am I missing?

1

There are 1 best solutions below

4
On

John,

I looked up seed with random and used epoch time. Solved.

millis = int(round(time.time())
random.seed(millis)
port = random.randint(50000,50999)

Ok almost certainly produces a random you could predict, but hey this isn't for the lottery or anything, its for a port number :)

Thanks you, you seeded that answer, forgive the pun.