Lua中math.random()和math.randomseed()之间的区别 [英] Difference between math.random() and math.randomseed() in Lua

查看:1489
本文介绍了Lua中math.random()和math.randomseed()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以Lua为编程语言的Corona SDK进行游戏.从表中获取随机数时,我很困惑使用以下哪一项?

I am working on game using Corona SDK with Lua as Programming Language . While getting random number from table , I am confused to use which one of following ?

math.random()
math.randomseed()

有人会详细说明它们之间的确切区别吗?这样我就可以使用最好的一个.

Will any one elaborate the exact difference between them ? So I can use the best one in my case.

推荐答案

如果调用math.random()时需要相同的顺序,则必须在调用math.random()之前设置math.randomseed(same number every time before calling).

If you need the same sequence when you call math.random(), then you must set math.randomseed(same number every time before calling) before calling math.random().

如果您想要不同的随机数序列,请在调用math.random()之前传递math.randomseed(os.time()),因为os.time()会返回以秒为单位的时间,因此每次都会有所不同.

If you want a different sequence of random numbers, then pass math.randomseed(os.time()) before calling math.random() because os.time() will return the time in number of seconds so it will be different every time.

请参考以下链接并在此处查看示例.

Please refer to following link and check example there.

https://docs.coronalabs.com/api/library/math/randomseed.html

这篇关于Lua中math.random()和math.randomseed()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆