pick 5 random units using group loops?

Flare

Stops copies me!
Reaction score
662
Just do a loop using integers. Each time it runs, use GroupPickRandomUnit and add that to a second group, and remove it from the first. Then you should have a new group formed with your required number of random units

JASS:
function test takes nothing returns nothing
    local real x = GetRandomReal (-500., 500.)
    local real y = GetRandomReal (-500., 500)
    local real radius = 500.
    local group group1 = CreateGroup ()
    local group group2 = CreateGroup ()
    local unit u
    local integer ticks = 1
    local integer maxticks = 5
    call GroupEnumUnitsInRange (group1, x, y, radius, Condition (function MyCondition))
        loop
        exitwhen ticks >= maxticks
        set u = GroupPickRandomUnit (group1)
        call GroupAddUnit (group2, u)
        call GroupRemoveUnit (group1, u)
        set ticks = ticks + 1
        endloop
//Do some stuff with group2
    call DestroyGroup (group1)
    call DestroyGroup (group2)
    set group1 = null
    set group2 = null
    set u = null
endfunction
 

N-a-z-g-u-l

New Member
Reaction score
30
okay, its an old thread, and its solved, but theres already a BJ doing exactly the same thing

well, the question was for a loop, but imho in this case its a good idea to use the BJ

JASS:
//===========================================================================
function GetRandomSubGroupEnum takes nothing returns nothing
    if (bj_randomSubGroupWant > 0) then
        if (bj_randomSubGroupWant >= bj_randomSubGroupTotal) or (GetRandomReal(0,1) < bj_randomSubGroupChance) then
            // We either need every remaining unit, or the unit passed its chance check.
            call GroupAddUnit(bj_randomSubGroupGroup, GetEnumUnit())
            set bj_randomSubGroupWant = bj_randomSubGroupWant - 1
        endif
    endif
    set bj_randomSubGroupTotal = bj_randomSubGroupTotal - 1
endfunction

//===========================================================================
function GetRandomSubGroup takes integer count, group sourceGroup returns group
    local group g = CreateGroup()

    set bj_randomSubGroupGroup = g
    set bj_randomSubGroupWant  = count
    set bj_randomSubGroupTotal = CountUnitsInGroup(sourceGroup)

    if (bj_randomSubGroupWant <= 0 or bj_randomSubGroupTotal <= 0) then
        return g
    endif

    set bj_randomSubGroupChance = I2R(bj_randomSubGroupWant) / I2R(bj_randomSubGroupTotal)
    call ForGroup(sourceGroup, function GetRandomSubGroupEnum)
    return g
endfunction


it definetely has a better performance than your script ;)

by the way, the code above already uses a BJ:

JASS:
function GroupPickRandomUnitEnum takes nothing returns nothing
    set bj_groupRandomConsidered = bj_groupRandomConsidered + 1
    if (GetRandomInt(1,bj_groupRandomConsidered) == 1) then
        set bj_groupRandomCurrentPick = GetEnumUnit()
    endif
endfunction

//===========================================================================
// Picks a random unit from a group.
//
function GroupPickRandomUnit takes group whichGroup returns unit
    // If the user wants the group destroyed, remember that fact and clear
    // the flag, in case it is used again in the callback.
    local boolean wantDestroy = bj_wantDestroyGroup
    set bj_wantDestroyGroup = false

    set bj_groupRandomConsidered = 0
    set bj_groupRandomCurrentPick = null
    call ForGroup(whichGroup, function GroupPickRandomUnitEnum)

    // If the user wants the group destroyed, do so now.
    if (wantDestroy) then
        call DestroyGroup(whichGroup)
    endif
    return bj_groupRandomCurrentPick
endfunction
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top