RPG Maker MZ - Randomizing Events and Preventing Duplicates

I am trying to have events randomized based upon a RAN1-4, but if 1 event has a 2 for example, one of the other 3 cannot.

For example, if I walked into a map there would be x4 random NPCs in different locations, but each time I enter they may not be there, or may be in another location. Below is how it should look (any combination of the 4)

Screenshot 2023-11-21 090413.png


However, what I am getting is different combinations:

Screenshot 2023-11-21 090327.png


I tried using Conditional Branches, but on tests the events still duplicated.

I found a great piece of code on here posted by @Shaz:

Screenshot 2023-11-21 085127.png


And thought I could set a 5th variable based upon the sum of the other 4, so if the event equals that sum all is good, if not roll again. But that is not working.

The chest is a trigger to reset the random roll, my Parallel Event, random event and variables are set up as follows:

Screenshot 2023-11-21 084957.png


Screenshot 2023-11-21 085333.png


Screenshot 2023-11-21 085449.png


Any help would be appreciated. Thanks.
There are a lot of ways you could do this, but what I would personally do is create an array that contains one of every number, and then shuffle that array. That would give you one of each number, randomly distributed. Then just place each value from the array into a variable. Here is a script command that could accomplish all of that:

JavaScript:
// You don't need to understand how this function works.
// You just need to know that it's a popular algorithm for shuffling an array.
function shuffle(array) {
    let currentIndex = array.length, temporaryValue, randomIndex;

    while (currentIndex !== 0) {
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;

        temporaryValue = array[currentIndex];
        array[currentIndex] = array[randomIndex];
        array[randomIndex] = temporaryValue;
    }

    return array;
}

// Make an array containing all possible results
let numbers = [1, 2, 3, 4];

// Shuffle the array
numbers = shuffle(numbers);

// For each game variable, remove the last element of the array and store it in the variable
$gameVariables.setValue(2, numbers.pop()); // Red
$gameVariables.setValue(3, numbers.pop()); // Blue
$gameVariables.setValue(4, numbers.pop()); // Green
$gameVariables.setValue(5, numbers.pop()); // Yellow

Just remove your five Control Variables commands, and insert a script command containing this code above the Control Self Switch command.
@Arthran You are a Star! Thank You.
Just to give a lower-level example of how you could do this with only event commands:
Code:
◆Control Variables:#0007 Char 1 = Random 1..4
◆Control Variables:#0008 Char 2 = Random 1..4
◆Control Variables:#0009 Char 3 = Random 1..4
◆Control Variables:#0010 Char 4 = Random 1..4
◆Loop
  ◆If:Char 2 ≠ Char 1
    ◆Break Loop
    ◆
  :Else
    ◆Control Variables:#0008 Char 2 = Random 1..4
    ◆
  :End
  ◆
:Repeat Above
◆Loop
  ◆If:Char 3 ≠ Char 1
    ◆If:Char 3 ≠ Char 2
      ◆Break Loop
      ◆
    :Else
      ◆Control Variables:#0009 Char 3 = Random 1..4
      ◆
    :End
    ◆
  :Else
    ◆Control Variables:#0009 Char 3 = Random 1..4
    ◆
  :End
  ◆
:Repeat Above
◆Loop
  ◆If:Char 4 ≠ Char 1
    ◆If:Char 4 ≠ Char 2
      ◆If:Char 4 ≠ Char 3
        ◆Break Loop
        ◆
      :Else
        ◆Control Variables:#0010 Char 4 = Random 1..4
        ◆
      :End
      ◆
    :Else
      ◆Control Variables:#0010 Char 4 = Random 1..4
      ◆
    :End
    ◆
  :Else
    ◆Control Variables:#0010 Char 4 = Random 1..4
    ◆
  :End
  ◆
:Repeat Above

Arthran's method is definitely more efficient, but this is a method you could do without any coding knowledge.

Original thread: https://forums.rpgmakerweb.com/threads/rpg-maker-mz-randomizing-events-and-preventing-duplicates.163131/