Lua中的随机数生成问题

Lua中的随机数生成问题,lua,Lua,我们执行的任务是接受一个数字作为math.randomseed()的种子值,并在循环的每次迭代期间使用math.random()生成一个随机整数(间隔[1,6]),然后继续循环,直到数字为6 我们已经为其编写了代码 i = io.read() local count = 0 math.randomseed(i) for x = 1, 4 do value = math.random(1, 6) print(value) count = count + 1 end pri

我们执行的任务是接受一个数字作为
math.randomseed()
的种子值,并在循环的每次迭代期间使用
math.random()
生成一个随机整数(间隔[1,6]),然后继续循环,直到数字为6

我们已经为其编写了代码

i = io.read()
local count = 0
math.randomseed(i)
for x = 1, 4 do
    value = math.random(1, 6)
    print(value)
    count = count + 1 
end
print(count)
我们没能通过考试,因为

    Input (stdin)
    Run as Custom Input
    0

    Your Output (stdout)
     3
     5
     5
     6
     2

    Expected Output
    3
    5
    5
    6
    4

请帮助我们

操作代码似乎有循环问题。我鼓励OP阅读Lua文档,其中介绍了Lua的
while
repeat to
goto
,以及以下关于Lua的手册部分。如果不了解任何语言的基本控制结构,就很难用任何语言编写任何程序

OP问题可以用多种方法解决

用于
这里的
math.ground
是Lua中最大的可表示数,通常是特殊值
inf
,这实际上是一个无限循环。变量
i
跟踪生成了多少个随机数,但由于
i
在循环外不可见,因此需要
count
变量来复制
i
的值,以便在最后打印

在 这里,测试在循环构造的末尾进行。这允许代码只调用一次
math.random

使用
goto

您应该运行循环,直到随机值等于6。因此,停止每2分钟更改for循环限制。请您阅读Lua手册好吗?如果你必须重复某件事直到某个条件为真,为什么不给repeat until语句一个机会呢?这是我们编写的最后一段代码,现在仍然不能作为问题使用…i=io.read()local count=0 local a=6 math.randomseed(i)for x=1,4 do value=math.random(1,6)repeat print(value)直到值-- for version i = io.read() math.randomseed(i) local count for i = 1, math.huge do local value = math.random(1, 6) print(value) count = i if value == 6 then break end end print(count)
-- while version
i = io.read()
math.randomseed(i)

local value = math.random(1, 6)
local count = 1
while value < 6 do
  print(value)
  count = count + 1
  value = math.random(1, 6)
end
print(value)
print(count)
-- repeat until version
i = io.read()
math.randomseed(i)

local count = 0
repeat
  local value = math.random(1, 6)
  print(value)
  count = count + 1
until value == 6
print(count)
-- goto version
i = io.read()
math.randomseed(i)

local count = 0
::loop::
local value = math.random(1, 6)
print(value)
count = count + 1
if value < 6 then goto loop end
print(count)