简体   繁体   English

MicroPython 中的 random 似乎可以做任何事情?

[英]random in MicroPython does appear to do anything?

Using pybricks-micropython使用 pybricks-micropython

Running this command to get a random port number, works under CPython.运行此命令以获取随机端口号,可在 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?我猜想 MicroPython 可能需要更多东西?

What am I missing?我错过了什么?

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.谢谢你,你播种了那个答案,原谅双关语。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM