• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Generating a list of random integers, each one different from the other one. How?

Status
Not open for further replies.
Level 3
Joined
Sep 11, 2008
Messages
58
I would like to create a list of integer values in a certain range, none of them equal to one of the others, in a random order. E.g. for a range of 1 to 5 it could create the values 4, 2, 1, 5, 3.

I tried it the following, unpleasing way:

JASS:
function create_unsorted_integer_list takes nothing returns nothing
    local integer array list
    local integer min = 0
    local integer max = 9
    local integer rnd
    local integer i = 0
    local integer j = 0
    local boolean exists = false
    
    set i = 0
    loop //loops through the list from 0 to max-min to set each index' value
        exitwhen i > max-min
        loop //loops until it hits a random int that doesn't exist in the list so far
            set rnd = GetRandomInt(min, max)
            set exists = false
            set j = 0
            loop //check if the random value doesn't exist in the list
                exitwhen j > i
                if rnd == list[j] then
                    set exists = true
                    exitwhen == true
                endif
                set j = j + 1
            endloop
            exitwhen exists == false
        endloop
        set list[j] = rnd //if the random value didn't exist in the list, it will be assigned to the list now
        set i = i + 1
    endloop
endfunction

Looks quite complex for such an easy purpose. But this is not my actual concern. It's time intensive! Especially if the range between min and max is high, and the closer the list is to completion.

E.g. if i want a list from 0 to 100, and 99 values are already set, and the value '5' is left to be set, it takes an average of 100 tries (and loops through the list for the check), until GetRandomInt hits the value '5'. And this for only the last value. (And imagine the range would be 1000 or even more.)

Anyone knows a better way to do this? Thanks for any answers.

whisp
 
Level 40
Joined
Dec 14, 2005
Messages
10,532
Here's how I would do it (Untested but it should be fine):

JASS:
globals
    integer array list //this is our output
endglobals

function getList takes integer min, integer max returns nothing
    local integer array temp
    local integer i = 0
    loop
        exitwhen i > max-min
        set temp[i] = i+min
        set i = i + 1
    endloop
    set min = 0
    loop
        exitwhen i < 0
        set max = GetRandomInt(0,i)
        set list[min] = temp[max]
        set temp[max] = temp[i]
        set i = i - 1
        set min = min + 1
    endloop
    //list should be good to go
endfunction
 
Last edited:
Level 3
Joined
Sep 11, 2008
Messages
58
Thanks for another great solution. I hope i'll be able to develop them as effective myself one day. I'd give you some rep, but i don't have any to give away yet...
 
Status
Not open for further replies.
Top