• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[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