从桌子上挑选选定数量的东西而不重复
我目前正在做一个小项目,这是我第一次学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您需要以 20% 的概率(从 1..5 范围中的一个)选择一个,并以 25% 的概率(从 1..5 范围中的一个减去第一个选择)选择第二个,那么类似这样的操作应该有效
:保证数字不相等并且满足您的标准。
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:
This will guarantee the numbers not being equal and satisfying your criteria.
您可以生成差值
i2 - i1
而不是生成 i2Instead of generating i2 you can generate difference
i2 - i1
您可以使用递归。保存之前的数字,如果相同则生成新的数字,直到不相同为止。这样您就可以保证永远不会出现相同的号码两次。
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.
示例:“2 from 5”,不带双打...
示例输出...
Example: "2 from 5" without doubles...
Example output...