• 🏆 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!

Help Randomize integer.

Status
Not open for further replies.
Level 2
Joined
Jul 14, 2019
Messages
13
Hello! I'm looking for help regarding randomizing integers without it repeating the same number.

So the goal is to set where people will spawn each round.
So far i've set up an Integer with an Array thinking it could hold all the different numbers so when the spawn trigger is activated it can go thru the array and select all the players spawn locations.

Its fairly easy to randomize the numbers, the tricky part is to "block" out numbers thats already taken.
Thats where I need your help. I've drawn a picture and hopefully it will help explain a bit further else for further information please leave a comment and I'll happily explain further.

https://imgur.com/hiHTiQE

Thanks in advance. :cool:2
 
Level 18
Joined
Jan 1, 2018
Messages
728
JASS:
globals
    integer count
    integer array values
endglobals

function InitValues takes nothing returns nothing
    set values[0] = 0
    set values[1] = 1
    ...
    set values[49] = 49
    set count = 50
endfunction

function PickRandomValue takes nothing returns integer
    local integer index = GetRandomInt(0, count - 1)
    local integer result = values[index]
    set values[index] = values[count - 1]
    set count = count - 1
    return result
endfunction
 
Level 2
Joined
Jul 14, 2019
Messages
13
I think I see what you're doing, would it be possible for you to "convert" this to baby version(GUI)? as I feel like I can experience with it more to try understand it further.

Gotta start somewhere..
 
Level 23
Joined
Apr 3, 2018
Messages
460
Basically to remove an item from your array, just replace it with the last element of the array and reduce the counter by 1.

  • pick random
    • Events
    • Conditions
    • Actions
      • -------- choose random element --------
      • Set random = (Random integer number between 0 and (count - 1))
      • Set result = array[random]
      • -------- remove the selected element from the array --------
      • Set count = (count - 1)
      • Set array[random] = array[count]
 
Level 2
Joined
Jul 14, 2019
Messages
13
Thank you both.

I feel like i've learned a lot from testing around with this and I (think) got it to work, just other issues to deal with now...

But I hope you both have a wonderful time. Much respect :thumbs_up:
 
Level 2
Joined
Jul 14, 2019
Messages
13
Seems like I still have one issue.

So when I "run" this trigger an array is set for example : 0 2 1 3.
Tho sometimes it might turn out to be 0 2 1 1 and I cant have repeating numbers. there cant be two 1's in the array as this is where each "team" will be spawned causing multiple teams to spawn in the same area.

I've copied Biridius trigger, made sure its correct and its a flawless one if repeating ones are okey.
Think the issue is "Set random = (Random integer number between 0 and (count - 1))" we only remove the highest number it can "roll" as. so the array can never be 3 3 X X as the 3 will be gone BUT it can technically roll 1 1 1 0.

Any advices or changes that can be done to the trigger to fix this issue? been trying to solve it now but seems like a complicated topic for someone new like me.

Thanks in advance!
 
Level 23
Joined
Apr 3, 2018
Messages
460
we only remove the highest number it can "roll"
When rolling, count-1 is used because when counting indexes from zero, the total count will be higher than the last element's index.
For example, 0 1 2 3 (indexes) with count = 4. If we now rolled random(0,4) and it returned 4, it would be an undefined value as there is no such element in the array.

I cant have repeating numbers.
I feel like you misunderstand the purpose of this trigger. It does not set up the array itself, it merely picks and removes one element out of it (the variable "result" is what you then use as your start location index or whatever).
You are supposed to first fill the array with all your non-repeating start location indexes with a loop, before running my trigger.
Then run my trigger from before each time you want to take one random element out of it.

(couldn't copy-paste it because of random cyrillics in my halfway-localised editor)
trigger.png

After testing a little, there could also be issues when one of the start locations belongs to an absent player. It seems to coincide with Red player's (0) start location. So if you are testing your map with only 2 players, you will only have 2 start locations to choose from, no matter how many of them are set up in the terrain editor, and some of them will coincide.
This could be the reason for you writing about "repeating numbers" if you have already set up the trigger like above.

The solution is probably to use manually set up locations through coordinates rather than normal "start locations" since you can't always have all player slots filled. That would require another array of type "location".
 
Last edited:
Level 2
Joined
Jul 14, 2019
Messages
13
I've Copied the exact trigger of Biridius, I just used it wrongly. I assumed it registered the numbers in the Array.
But thanks for showing me how to post my Triggers, will come handy in the future!

When rolling, count-1 is used because when counting indexes from zero, the total count will be higher than the last element's index.
For example, 0 1 2 3 (indexes) with count = 4. If we now rolled random(0,4) and it returned 4, it would be an undefined value as there is no such element in the array.


I feel like you misunderstand the purpose of this trigger. It does not set up the array itself, it merely picks and removes one element out of it (the variable "result" is what you then use as your start location index or whatever).
You are supposed to first fill the array with all your non-repeating start location indexes with a loop, before running my trigger.
Then run my trigger from before each time you want to take one random element out of it.

(couldn't copy-paste it because of random cyrillics in my halfway-localised editor)
View attachment 367455
After testing a little, there could also be issues when one of the start locations belongs to an absent player. It seems to coincide with Red player's (0) start location. So if you are testing your map with only 2 players, you will only have 2 start locations to choose from, no matter how many of them are set up in the terrain editor, and some of them will coincide.
This could be the reason for you writing about "repeating numbers" if you have already set up the trigger like above.

The solution is probably to use manually set up locations through coordinates rather than normal "start locations" since you can't always have all player slots filled. That would require another array of type "location".

I see, I thought we registered the "result" numbers in the Array after, turning it into a list and I used the Array[0] to determan the player 1's location, Array[1] for player 2 and so on. But I see the Issue I was doing. Thanks for clarifying it!

Thank you!
 
Status
Not open for further replies.
Top