Any clever kids, here? Help me solve how to randomly generate variable results without duplicates.

Hello everybody!

So I asked for help in the maker forums because I need to be able to identify potions by consuming them. The potions' effects are randomized each time you get in the dungeon. So you have 5 potions, 5 different effects, first time in the dungeon, the Red Potion heals you, second time in, Red Potion poisons you, etc. However, whenever you identified a Potion, it stays identified for the duration of the run, so every time you should be getting a Red Potion, you get a Healing Potion instead if it's what its been assigned to.

Everybody still with me? Good.

Now, I wrote some voodoo to help me initialize the whole boomshalala and get the potions assigned. It doesn't work, of course. I can't quite figure out how to ensure that each potion gets a different effect assigned. Then how to make it so that when it's consumed, it "transforms" into the Potion with the effect it should have.

Here's my gibberish:
Code:
function PotionSetup() {
//this function gets a random value and you can set a deleteFlag in order to decide whether to remove that from the given array
var GetRandomElement = function(myArray) {
   var randomItem = myArray[Math.floor(Math.random()*myArray.length)];
}

//Call PotionSetup() function in order to mix the potions.
var PotionMixer = function() {

   var PotMix = new Array();
    for (var i=1; i<5; ++i) {
      PotMix.push(i);
   }
   $gameVariables.setValue(20, GetRandomElement(PotMix[1]));
   $gameVariables.setValue(21, GetRandomElement(PotMix[2]));
   $gameVariables.setValue(22, GetRandomElement(PotMix[3]));
   $gameVariables.setValue(23, GetRandomElement(PotMix[4]));
   $gameVariables.setValue(24, GetRandomElement(PotMix[5]));
}
 }

Anyone got fifteen minutes and up for a small challenge? I really appreciate it!
You don't want to initialize arrays as objects.
Just put in PotMix = [];

Also, your call won't work because PotMix[1] isn't an array, it's a number.
PotMix will be [1,2,3,4,5], as per your call, so calling your first function on PotMix[1] (=1) makes no sense.

Your first function will also return nothing since you didn't put in a return statement, but only an assignation.

I suggest you heavily brush up on array and function fundamentals.

Anyway, the solution to your problem is to return the element that gets picked and remove it from the array.
When you pick the random element save it to a variable, then remove it using the splice function from the PotMix array, using indexOf.

Edit: Written solution in spoiler below. Don't look at it until after you've tried or you won't learn.
Spoiler
Code:
var PotMix = [1,2,3,4,5];
var GetRandomElement = function(myArray) {
   return myArray[Math.randomInt(myArray.length)];
}
var length = PotMix.length
for (var i = 0; i < length; i ++) {
   var num = GetRandomElement(PotMix)
   $gameVariables.setValue(10 + i,num);
   PotMix.splice(PotMix.indexOf(num),1)
}
Thanks a lot for your help, @Astfgl66.

Yeah, I read your answer before edit, and got a new idea. Still wonky, though.
Here's the code I rewrote:
Code:
 var PotMix = [];

        function generateNumbers()
        {
          
            for(var i=0; i<5; i+=1)
            {
                PotMix.push(i);
            }
        }

        function GetRandomElement()
        {
            if(PotMix.length==0)
            {
               generateNumbers();
            }
            var rand = Math.floor(Math.random()*PotMix.length);
            var num = PotMix[rand];
            PotMix.splice(rand,1);
          
          
           $gameVariables.setValue(20, num[1]);
           $gameVariables.setValue(21, num[2]);
           $gameVariables.setValue(22, num[3]);
           $gameVariables.setValue(23, num[4]);
           $gameVariables.setValue(24, num[5]);
        }

I was scratching my head on why didn't the variables get filled when I saw your solution. I'm gonna keep yours on deck in case I really don't find anything, no offense, I appreciate you doing that I really do, but I want to try and solve this my way before.

It seems like I'm gonna have to use it, though, so you didn't work for nothing :'D
Note that variables won't be filled if you don't have them set in your editor.
If your max number of variables is 20, then 21 to 24 won't be filled.
It might seem obvious, but it's caused me to stumble a couple times.
Yes, I thought of that. I set them up for a few dozens for now.

I still can't get them to fill. They always return 0.

EDIT:

Sorry. but I gave up. I used your solution in a script call straight from an event. I'll call the event every time the player is to enter the dungeon and that's that. It does the job beautifully. I can't figure out how to set the game variables straight from the plugins, so let's leave that for now until I get some dollars to commission a full fledged Item Randomizer & Identifier from one of you guys and give it to the community.

Thanks again for your time. I appreciate it.
Run through your code on paper. Write the results as they would appear, without interpretation, line by line. You'll see where you went wrong.

Spoiler
Code:
var PotMix = []; //PotMix is now []
       function generateNumbers() // forgot to put a var here, this function is now global
       {
           for(var i=0; i<5; i+=1)
           {
               PotMix.push(i);
           }
       } // function defined

       function GetRandomElement() ///once again forgot to put a var, this function is now global
       {
           if(PotMix.length==0) //this is true PotMix is []
           {
              generateNumbers(); //runs function above PotMix is now [1,2,3,4,5]
           }
           var rand = Math.floor(Math.random()*PotMix.length); // lets say rand is equal to 1
           var num = PotMix[rand]; //num is now equal to PotMix[1] > 2
           PotMix.splice(rand,1); //you remove element 2 from PotMix it's now [1,2,4,5]
       
       
          $gameVariables.setValue(20, num[1]); // num is 2 and not an array
          $gameVariables.setValue(21, num[2]); //same
          $gameVariables.setValue(22, num[3]);
          $gameVariables.setValue(23, num[4]);
          $gameVariables.setValue(24, num[5]);
       }

You're confusing array and numbers again. This is because you have no idea what you're writing again.
Once again: make your code logic on paper, then write the code.
Oh but I disagree. I did not and will not give you permission to use the ready made version as a final product. You will go through and debug your code, and you will learn. Code is not magic, it's logic. Focus. I just went through your code above in the spoilers, see where you went wrong and go from there.

Code logic
Spoiler
Define your array
Define your function to get a random element from the array

for as long as the array length
get a random element from the array and assign it to a variable
remove that element from the array
end loop

Then translate this into code.
Oh damnit.

This is the full code I have on my .js file:

Code:
function GetRandomElement()
        {
           var PotMix = [];
            if(PotMix.length==0)
               {
                       for(var i=0; i<5; i+=1)
                   {
                PotMix.push(i);
                   }
             else if {
                return;
                       {
               }

            var rand = Math.floor(Math.random()*PotMix.length);
            var num = PotMix[rand];
            PotMix.splice(rand,1);
           
           $gameVariables.setValue(20, num[1]);
           $gameVariables.setValue(21, num[2]);
           $gameVariables.setValue(22, num[3]);
           $gameVariables.setValue(23, num[4]);
           $gameVariables.setValue(24, num[5]);
       }

Now, let's go through it. First of all, I need an array to store my numbers, right? Here is var PotMix[];. This is my container. It's empty, now. But I'm never sure because machines always up, so I tell it to check if the array is empty before starting to do anything. If it's not, do nothing, keep it like that. If it's empty, fill it with 5 numbers. For each number you fill, push it and extend the array size.

Now that's set up. Let's get to work and get the numbers proper. Choose 5 random numbers from 1 to 5. Store them into a new array based on the first array. Check if you find any duplcates, remove them and choose another number, until all 5 numbers are in.

Once that's over, let's put those jumbled numbers into variables. The first variable, which Variable[0020] will contain the first number from the num array and the second variable will contain the second... etc.

There it is. I don't see where I went wrong. It's supposed to work. It doesn't.
But this is not what happens. You're interpreting and not executing literally like your computer.
Read my spoilers from 30 minutes ago.

Original thread: https://forums.rpgmakerweb.com/threads/any-clever-kids-here-help-me-solve-how-to-randomly-generate-variable-results-without-duplicates.102602/