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:
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
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