简体   繁体   English

如何使用 Random.Range 的答案将最大值设置为相同的 Random.Range,直到达到 1

[英]How can I use the Random.Range's answer to set as the max value into the same Random.Range until it reaches 1

I want to create a script that generates a random number (between 1(min) and 9999(max)) and then I want the random number to become the new max for the same exact script.我想创建一个生成随机数(介于 1(min)和 9999(max)之间)的脚本,然后我希望随机数成为相同脚本的新最大值。

Is there a way to create an equation or some sort of way to do this infinitely (until 1).有没有办法创建一个方程或某种方法来无限地做到这一点(直到 1)。

The only solution I have come up with is this:我想出的唯一解决方案是:

private IEnumerator MyCoroutine()
{
    var a  = Random.Range(1, 9999 + 1);
    largeText.text = a.ToString();
    yield return new WaitForSeconds(2);
    if (a > 1)
    {
        var b  = Random.Range(1, a + 1);
        largeText.text = b.ToString();
        yield return new WaitForSeconds(2);
        if (b > 1)
        {
            //Then continues 'infinitely'
        }
    }
}

This is a solution, but I eventually run into the problem of having an extremely unwanted script.这是一个解决方案,但我最终遇到了一个非常不需要的脚本的问题。

Ofcourse I have the rest that I need with this script, and it does work, however I would like to know if there is an easier work around that doesn't involve me writing 1m lines of code.当然,我有这个脚本需要的 rest,它确实有效,但是我想知道是否有更简单的解决方法,不需要我编写 1m 行代码。

Thank you!谢谢!

Simply re-use the same variable instead of creating a new one for each "iteration" and use a while loop:只需重复使用相同的变量,而不是为每个“迭代”创建一个新变量并使用while循环:

private IEnumerator MyCoroutine()
{
    var currentRandom = 9999;

    while(currentRandom > 1)
    {
        currentRandom = Random.Range(1, currentRandom + 1)
        largeText.text = currentRandom.ToString();
        yield return new WaitForSeconds(2);
    }
}

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

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