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

查看:132
本文介绍了具有step选项的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(第一,最后)相同的行为,不同之处在于值之间的距离为"stepSize".请注意,最高随机数可能不是"last",这取决于(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.

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

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