带有步进选项的 math.random 函数? [英] math.random function with step option?

查看:17
本文介绍了带有步进选项的 math.random 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个自定义函数,它将返回一个随机数,其中包含一个可用的步骤选项,例如 for 循环.示例:

A custom function that will return a random number with a step option available like in the for loop. Example:

for i=1,10,**2** do
   print(i)
end

推荐答案

你的意思是:

function randomWithStep(first, last, stepSize)
    local maxSteps = math.floor((last-first)/step)
    return first + stepSize * math.random(0, maxSteps)
end

这给出了与 math.random(first, last) 相同的行为,除了值将是stepSize"分开.请注意,最高随机数可能不是最后一个",这取决于 (last-first) 是否是 stepSize 的倍数.

This gives the same behavior as math.random(first, last) except that the values will be "stepSize" apart. Note that the highest random # may not be "last", depends if (last-first) is a multiple of stepSize.

这篇关于带有步进选项的 math.random 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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