从表中获取一个值并将其分配给另一个值,无需重复 [英] Getting one value from a table and assigning it to another with no repeats
问题描述
具体来说,这是针对Garry's Mod的,但是我认为在这个问题上没有太大的意义.我想要做的是得到一个玩家,并将他们的值设置为另一位随机玩家(因此,每个玩家都有一个随机的目标").我想做到这一点而无需重复,这样就不会将玩家分配给自己.为了更好地说明:
与这张图片的唯一区别是,我希望将每个玩家分配给另一个随机玩家,所以更像是玩家1 =>玩家5,玩家3 =>玩家2,等等.>
此刻是我的代码,但这总是使一个人未被选中:
validTargets = {}
TargetList = {}
local Swap = function(array, index1, index2)
array[index1], array[index2] = array[index2], array[index1]
end
GetShuffle = function(numelems)
local shuffle = {}
for i = 1, numelems do
shuffle[#shuffle + 1] = i
end
for ii = 1, numelems do
Swap(shuffle, ii, math.random(ii, numelems))
end
return shuffle
end
function assignTargets()
local shuffle = GetShuffle(#playing)
for k,v in ipairs(shuffle) do
TargetList[k] = v
end
SyncTargets()
end
function SyncTargets()
for k,v in pairs(TargetList) do
net.Start("sendTarget")
net.WriteEntity(v)
net.Send(k)
end
end
我有一个lua函数,在给定n
的情况下,该函数会生成从1到n
的数字随机洗牌.
该方法基于热门算法生成的随机排列元素数组.
您可以尝试像这样使用
:local Swap = function(array, index1, index2)
array[index1], array[index2] = array[index2], array[index1]
end
GetShuffle = function(numelems)
local shuffle = {}
for i = 1, numelems do
shuffle[#shuffle + 1] = i
end
for ii = 1, numelems do
Swap(shuffle, ii, math.random(ii, numelems))
end
return shuffle
end
function assignTargets()
local shuffle = GetShuffle(#playing) --assuming `playing` is a known global
for k,v in ipairs(shuffle) do
TargetList[k] = v
end
end
Specifically, this is for Garry's Mod, however I don't think that matters too much in this problem. What I want to do is get one player, and set their value to another random player (so every player has a random 'target'). I want to do this with no repeats, and so that a player is not assigned to their self. To better illustrate:
The only difference to that picture is that I want each player to be assigned to another random player, so more like player1 => player 5, player 3 => player 2, etc.
Here is my code at the moment, however this always leaves one person unpicked:
validTargets = {}
TargetList = {}
local Swap = function(array, index1, index2)
array[index1], array[index2] = array[index2], array[index1]
end
GetShuffle = function(numelems)
local shuffle = {}
for i = 1, numelems do
shuffle[#shuffle + 1] = i
end
for ii = 1, numelems do
Swap(shuffle, ii, math.random(ii, numelems))
end
return shuffle
end
function assignTargets()
local shuffle = GetShuffle(#playing)
for k,v in ipairs(shuffle) do
TargetList[k] = v
end
SyncTargets()
end
function SyncTargets()
for k,v in pairs(TargetList) do
net.Start("sendTarget")
net.WriteEntity(v)
net.Send(k)
end
end
I have a lua function that generates a Random shuffle of numbers from 1 to n
, given n
.
The method is based on a popular algorithm to generate a random permutation of an array of elements .
You can try using that like so:
local Swap = function(array, index1, index2)
array[index1], array[index2] = array[index2], array[index1]
end
GetShuffle = function(numelems)
local shuffle = {}
for i = 1, numelems do
shuffle[#shuffle + 1] = i
end
for ii = 1, numelems do
Swap(shuffle, ii, math.random(ii, numelems))
end
return shuffle
end
function assignTargets()
local shuffle = GetShuffle(#playing) --assuming `playing` is a known global
for k,v in ipairs(shuffle) do
TargetList[k] = v
end
end
这篇关于从表中获取一个值并将其分配给另一个值,无需重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!