The following is not fully tested, but it targets a random group member when you use /click TargetRandomGroupMember in a macro (can use it just before a spell cast). It won’t/can’t choose only people in range and it won’t prevent choosing the same person twice. To make it for mage-only, change the isTargetable function as mentioned in the comments:
-- This creates a secure button to target a random group (raid or party) member
-- To use: /click TargetRandomGroupMember
-- To be more selective on who to target, update the function isTargetable()
-- return true if the given unit ("raid22","party3") should be a candidate for targeting
local function isTargetable(unit)
return true -- change to return UnitClass(unit)=="Mage" to target only random mages
end
-- generates a list of units as attributes randomUnit1, randomUnit2, ..., randomUnit[randomNumUnits]
-- that are the units to target, such as randomUnit1="raid3", randomUnit2="raid14", etc
local function updateUnits(button)
-- can't set unit attributes in combat
if InCombatLockdown() then
return
end
button:SetAttribute("randomNumUnits",0)
-- start building list of units again
local numUnits = 0
local groupType = IsInRaid() and "raid" or "party"
for i=1,groupType=="raid" and 40 or 5 do
local unit = (groupType=="party" and i==5) and "player" or groupType..i
if UnitExists(unit) and isTargetable(unit) then
numUnits = numUnits + 1
button:SetAttribute("randomUnit"..numUnits,unit)
end
end
button:SetAttribute("randomNumUnits",numUnits)
end
-- /click TargetRandomGroupMember will click this button that's never made visible
local button = CreateFrame("Button", "TargetRandomGroupMember", UIParent, "SecureActionButtonTemplate")
-- set up events so updateUnits is called when logging in or the group changes
button:RegisterEvent("PLAYER_LOGIN")
button:RegisterEvent("GROUP_ROSTER_UPDATE")
button:SetScript("OnEvent",updateUnits)
-- the /click will target a to-be-chosen unit (chosen in the pre-click wrap below)
button:SetAttribute("type","target")
-- set up pre-click wrap to pick a random unit (or the player if no candidates)
SecureHandlerWrapScript(button,"OnClick",button,[[
local numUnits = self:GetAttribute("randomNumUnits") or 0
self:SetAttribute("unit",numUnits>0 and self:GetAttribute("randomUnit"..random(numUnits)) or nil)
]])
Paste the above code to addon.bool.no to turn it into an addon.
If an addon author wants to give it a shot, you’re welcome to take this code and create your own /targetrandom slash command that will take a list of names (or classes) as arguments, and then add a /click to the macro immediately after the unsecure /targetrandom. (The addon Select does this if you need an example how.)