MZ Random initial select for attack/skill?

So, by default, when you select an action, the game defaults to the last actor/enemy you selected previously. Is there a way to have a plugin/code that can, when called, make the initial selection for a given skill be randomly between the group of actors/enemies?
Like, not that a certain selection would be forced, but just if one were to just spam attack, they would hit a variety of targets instead of the same one.
I'm pretty sure that, by default, the selection starts on the first eligible target (index 0) in both cases? Maybe a plugin you're using changes that.

Regardless, to randomise it, this may work (untested):
Spoiler: plugin code
JavaScript:
/*:
 * @target MZ
 * @plugindesc Random initial target selection for battle actions.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/164822/
 * @help Free to use and/or modify for any project, no credit required.
 */
;void (alias => {
  Scene_Battle.prototype.startEnemySelection = function() {
    alias.apply(this, arguments);
    const w = this._enemyWindow;
    const L = w.maxItems();
    if (L)
      w.select(Math.randomInt(L));
  };
})(Scene_Battle.prototype.startEnemySelection);

void (alias => {
  Scene_Battle.prototype.startActorSelection = function() {
    alias.apply(this, arguments);
    const a = BattleManager.inputtingAction();
    const c = a.itemTargetCandidates().filter(t => t.isActor());  // jic
    const L = c.length;
    if (L)
      this._actorWindow.forceSelect(c[Math.randomInt(c.length)].index());
  };
})(Scene_Battle.prototype.startActorSelection);
I'm pretty sure that, by default, the selection starts on the first eligible target (index 0) in both cases? Maybe a plugin you're using changes that.

Regardless, to randomise it, this may work (untested):
Spoiler: plugin code
JavaScript:
/*:
 * @target MZ
 * @plugindesc Random initial target selection for battle actions.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/164822/
 * @help Free to use and/or modify for any project, no credit required.
 */
;void (alias => {
  Scene_Battle.prototype.startEnemySelection = function() {
    alias.apply(this, arguments);
    const w = this._enemyWindow;
    const L = w.maxItems();
    if (L)
      w.select(Math.randomInt(L));
  };
})(Scene_Battle.prototype.startEnemySelection);

void (alias => {
  Scene_Battle.prototype.startActorSelection = function() {
    alias.apply(this, arguments);
    const a = BattleManager.inputtingAction();
    const c = a.itemTargetCandidates().filter(t => t.isActor());  // jic
    const L = c.length;
    if (L)
      this._actorWindow.forceSelect(c[Math.randomInt(c.length)].index());
  };
})(Scene_Battle.prototype.startActorSelection);

This is almost precisely what I needed, thank you!
Is there a way to edit this to be more of a "per use/turn" basis? As is, it seems to only work as "apply once, affect the entire battle". At least when I put it onto a Common Event in a Troop's Battle Events.
Not that the latter is a huge problem, but I'd just like some finer control.
Edit: It's slightly worse than I expected; once applied to a battle, it stays on for future battles where it isn't even in any Events. Is there just a way to turn it "off" and go back to the (index0) indefinitely or until called again?
It is intended for use as a plugin:
  1. Copy+paste into a text editor (e.g. Notepad)
  2. Save As > File Type: All Files, Filename: whatever.js
  3. Import as a plugin through the Plugin Manager
  4. Save your project to apply Plugin Manager changes
  5. Test!

Yes, it applies to all actor actions in all battles. If you want it only for specific Skills/Items, you could try this instead (untested):
Spoiler: plugin code v2 (per item)
JavaScript:
/*:
 * @target MZ
 * @plugindesc Random initial target selection for battle actions.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/164822/
 * @help Only affects Skills/Items with this tag in their Note field:
 *    <randomSelect>
 *
 * Free to use and/or modify for any project, no credit required.
 */
;void (() => {
'use strict';

  const TAG_NAME = "randomSelect";

  const isTagged = function(action = BattleManager.inputtingAction()) {
    if (typeof action?.item !== "function")
      return false;
    return action.item().meta[TAG_NAME];
  };

  void (alias => {
    Scene_Battle.prototype.startEnemySelection = function() {
      alias.apply(this, arguments);
      if (isTagged()) {
        const w = this._enemyWindow;
        const L = w.maxItems();
        if (L)
          w.select(Math.randomInt(L));
      }
    };
  })(Scene_Battle.prototype.startEnemySelection);

  void (alias => {
    Scene_Battle.prototype.startActorSelection = function() {
      alias.apply(this, arguments);
      if (isTagged()) {
        const a = BattleManager.inputtingAction();
        const c = a.itemTargetCandidates().filter(t => t.isActor());  // jic
        const L = c.length;
        if (L)
          this._actorWindow.forceSelect(c[Math.randomInt(c.length)].index());
      }
    };
  })(Scene_Battle.prototype.startActorSelection);

})();
Again, this is a plugin: follow the numbered steps above to use it in your project.
It is intended for use as a plugin:
  1. Copy+paste into a text editor (e.g. Notepad)
  2. Save As > File Type: All Files, Filename: whatever.js
  3. Import as a plugin through the Plugin Manager
  4. Save your project to apply Plugin Manager changes
  5. Test!

Yes, it applies to all actor actions in all battles. If you want it only for specific Skills/Items, you could try this instead (untested):
Spoiler: plugin code v2 (per item)
JavaScript:
/*:
 * @target MZ
 * @plugindesc Random initial target selection for battle actions.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/164822/
 * @help Only affects Skills/Items with this tag in their Note field:
 *    <randomSelect>
 *
 * Free to use and/or modify for any project, no credit required.
 */
;void (() => {
'use strict';

  const TAG_NAME = "randomSelect";

  const isTagged = function(action = BattleManager.inputtingAction()) {
    if (typeof action?.item !== "function")
      return false;
    return action.item().meta[TAG_NAME];
  };

  void (alias => {
    Scene_Battle.prototype.startEnemySelection = function() {
      alias.apply(this, arguments);
      if (isTagged()) {
        const w = this._enemyWindow;
        const L = w.maxItems();
        if (L)
          w.select(Math.randomInt(L));
      }
    };
  })(Scene_Battle.prototype.startEnemySelection);

  void (alias => {
    Scene_Battle.prototype.startActorSelection = function() {
      alias.apply(this, arguments);
      if (isTagged()) {
        const a = BattleManager.inputtingAction();
        const c = a.itemTargetCandidates().filter(t => t.isActor());  // jic
        const L = c.length;
        if (L)
          this._actorWindow.forceSelect(c[Math.randomInt(c.length)].index());
      }
    };
  })(Scene_Battle.prototype.startActorSelection);

})();
Again, this is a plugin: follow the numbered steps above to use it in your project.
Understood, thank you again!!
You're welcome, hopefully that works for you! Happy RPG Making~ 👋

Original thread: https://forums.rpgmakerweb.com/threads/random-initial-select-for-attack-skill.164822/