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

Random Unit in Group

Status
Not open for further replies.
Level 3
Joined
May 12, 2015
Messages
32
So I have been mapping like always till i found a weird bug.

  • Set UnitVariable = (Random unit from UnitGroup)
This doesn't always (mb never?) give a random unit from a group. It is mostly notable when you add units to this group individually as it will always return the last added unit to the unit group.

Any efficent and correct way to get a random unit from a group?
 
Level 12
Joined
May 22, 2015
Messages
1,051
I'm not sure why it would do that. I may run a test next time I am in the editor. Looking at the code for it in blizzard.j (JASS Manual: API Browser - Blizzard.j) it uses an algorithm that is definitely properly random. The only way it would fail is if the random function doesn't work properly.

Can you show your trigger where you are using this?

Additionally, the way Jake Kessler does it will work. It might seem expensive but actually it is probably still going to be a little faster than the blizzard.j function (they both require to loop over the whole unit group but the blizzard.j function generates a random number for each iteration rather than only once at the end).

If you really needed performance, the best way might be to use an array or hashtable. You would need to figure out how to remove units properly, though, which will be a lot of work. Anyway, the end result is that you could choose a random unit in O(1) time (essentially fastest possible). I doubt there would be need for all of this, though. You'd need to have a lot of units in the group and be selecting units out of it randomly very often for it to be necessary.
 
Level 13
Joined
May 10, 2009
Messages
868
Chaosy is right, that could be the reason why the Random Unit made by Blizzard is always returning the same unit for you. File -> Preferences... -> Test Map (tab). Then, uncheck "Use Fixed Random Seed".

upload_2018-2-9_15-27-52.png
 
Status
Not open for further replies.
Top