从桌子上挑选选定数量的东西而不重复

发布于 2025-01-11 04:40:00 字数 368 浏览 0 评论 0原文

我目前正在做一个小项目,这是我第一次学习 LUA,目前陷入困境。所以我想做的是创建一个函数,随机选择 1 到 5 之间的两个数字,并使它们不会与玩家发生碰撞。我似乎无法随机选择两个数字而不使它们相同。我一直在环顾四周,但未能找到明确的答案。任何帮助将不胜感激!

到目前为止我的代码:

local function RandomChoice1()
    local t = {workspace.Guess1.CB1,workspace.Guess1.CB2,workspace.Guess1.CB3,workspace.Guess1.CB4,workspace.Guess1.CB5}
    local i = math.random(1,5)

end

So I'm currently working on a little side project, so this is my first time learning LUA and I'm currently stuck. So what I'm trying to do is create a function that will randomly choose two numbers between 1 and 5 and make it so they can not collide with the player. I can not seem to get the ability to chose two numbers at random without them being the same. I've been looking around, but have not been able to find a clear answer. Any help would be much appreciated!

My code so far:

local function RandomChoice1()
    local t = {workspace.Guess1.CB1,workspace.Guess1.CB2,workspace.Guess1.CB3,workspace.Guess1.CB4,workspace.Guess1.CB5}
    local i = math.random(1,5)

end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

对不⑦ 2025-01-18 04:40:00

如果您需要以 20% 的概率(从 1..5 范围中的一个)选择一个,并以 25% 的概率(从 1..5 范围中的一个减去第一个选择)选择第二个,那么类似这样的操作应该有效

local i1 = math.random(1,5) -- pick one at random from 1..5 interval
-- shift the interval up to account for the selected item
local i2 = math.random(2,5) -- pick one at random from 2..5 interval
-- assign 1 in case of a collision
if i2 == i1, then i2 = 1 end

:保证数字不相等并且满足您的标准。

If you need to select one with probability 20% (one from 1..5 range) and the second one with probability 25% (one from 1..5 range minus the first choice), then something like this should work:

local i1 = math.random(1,5) -- pick one at random from 1..5 interval
-- shift the interval up to account for the selected item
local i2 = math.random(2,5) -- pick one at random from 2..5 interval
-- assign 1 in case of a collision
if i2 == i1, then i2 = 1 end

This will guarantee the numbers not being equal and satisfying your criteria.

〃安静 2025-01-18 04:40:00

您可以生成差值 i2 - i1 而不是生成 i2

local i1 = math.random(5)   -- pick one at random from 1..5 interval
local diff = math.random(4) -- pick one at random from 1..4 interval
local i2 = (i1 + diff - 1) % 5 + 1  -- from 1..5 interval, different from i1
print(i1, i2)

Instead of generating i2 you can generate difference i2 - i1

local i1 = math.random(5)   -- pick one at random from 1..5 interval
local diff = math.random(4) -- pick one at random from 1..4 interval
local i2 = (i1 + diff - 1) % 5 + 1  -- from 1..5 interval, different from i1
print(i1, i2)
哭了丶谁疼 2025-01-18 04:40:00

您可以使用递归。保存之前的数字,如果相同则生成新的数字,直到不相同为止。这样您就可以保证永远不会出现相同的号码两次。

local i = 0;

function ran(min,max)
 local a = math.random(min,max);

  if (a == i) then
    return ran(min,max);
  else
    i = a;
    return a;
  end
end

You could use recursion. Save the previous number and if it's the same just generate a new one until its not the same. This way you are garaunteed to never have the same number twice.

local i = 0;

function ran(min,max)
 local a = math.random(min,max);

  if (a == i) then
    return ran(min,max);
  else
    i = a;
    return a;
  end
end
无戏配角 2025-01-18 04:40:00

示例:“2 from 5”,不带双打...

local t = {}

for i = 1, 5 do
  t[i] = i                               
end

-- From now a simple table.remove()...
-- ( table.remove() returns the value of removed key/value pair )
-- ...on a random key avoids doubles

for i = 1, 2 do
  print(table.remove(t, math.random(#t)))
end

示例输出...

1
4

Example: "2 from 5" without doubles...

local t = {}

for i = 1, 5 do
  t[i] = i                               
end

-- From now a simple table.remove()...
-- ( table.remove() returns the value of removed key/value pair )
-- ...on a random key avoids doubles

for i = 1, 2 do
  print(table.remove(t, math.random(#t)))
end

Example output...

1
4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文