• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] Random non-repeating in a loop

Status
Not open for further replies.
Level 4
Joined
Sep 12, 2008
Messages
111
How do I make random non-repeating integers in a loop? I'm doing this for the rect array which is based on the integer. Let's say for example: R | B | T and I want it to be randomized in any order but each player having exactly 1 region: B | T | R.
JASS:
scope firststage initializer Init

globals
rect array Correct
endglobals

private function Actions takes nothing returns nothing
    local integer array numbers
    local integer arrLen = 7
    local integer temp = 0
    local integer rndIndex = 0
    local integer index = 0
    local unit array u
    local playercolor array p
    set p[0] = PLAYER_COLOR_RED
    set p[1] = PLAYER_COLOR_BLUE
    set u[0] = gg_unit_n000_0000
    set u[1] = gg_unit_n000_0001
    set u[2] = gg_unit_n000_0002
    set u[3] = gg_unit_n000_0003
    set u[4] = gg_unit_n000_0004
    set u[5] = gg_unit_n000_0005
    set u[6] = gg_unit_n000_0006
    set u[7] = gg_unit_n000_0007
    loop
        exitwhen( index == arrLen )
        set numbers[ index ] = index
        set index = index + 1
    endloop
    set index = 0
    loop
        exitwhen( index == arrLen )
        set rndIndex = GetRandomInt( 0, arrLen )
        debug call BJDebugMsg(I2S(numbers[index]))
        call SetRect(Correct[numbers[rndIndex]], GetRectMinX(gg_rct_1stage), GetRectMinY(gg_rct_1stage), GetRectMaxX(gg_rct_1stage), GetRectMaxY(gg_rct_1stage))
        set temp = numbers[ index ]
        call SetUnitOwner(u[numbers[rndIndex]], Player(numbers[rndIndex]), true)
        set numbers[ index ] = numbers[ rndIndex ]
        set numbers[ rndIndex ] = temp
        set index = index + 1
    endloop
endfunction

private function Init takes nothing returns nothing
    local trigger t = CreateTrigger(  )
    call TriggerRegisterTimerEvent(t, 0.4, false)
    call TriggerAddAction( t, function Actions )
endfunction

endscope
 
Status
Not open for further replies.
Top