MV Randomizing a shop inventory using different integers.

Hello. So for my game, I'm currently working on randomized shops. When the player hits New Game, he/she is given a choice on how they want shops set up for their game, and there's a few different options. So far, I'm using this script, which randomizes a shop using a minimum item number and a max item number:

JavaScript:
◆Script:var id = 26, type = 0, min = 1, max = 20, inv = 10;
:      :
:      :var array = [], temp = [], rand;
:      :for(i = 0; i < inv; i++) {
:      :do { rand = Math.floor(Math.random()*(max-min+1)+min);
:      :} while (temp.indexOf(rand) > -1);
:      :temp[i] = rand;
:      :array[i] = [type, rand, 0];
:      :}
:      :if(Array.isArray($gameVariables.value(id)))
:      :   var array = $gameVariables.value(id).concat(array);
:      :$gameVariables.setValue(id, array);

Basically, in this example, the variable 26 is set to Item type, and randomly chooses between Item 1 and Item 20 to add to the shop, with a max of 10 items total chosen for that shop. No problem, as it works fine.

My question is this: Is it possible, instead of making the variable choose between a min number and a max number, to have that same variable choose 10 random items (for example) from an array of a number of different integers? So, instead of choosing between 1 and 20, is there a way of having it choose between something like, for example, 1, 5, 13, 15, 17, 19, 21, 25, 29 and 40? I'm curious how would the code be written in this case?

Thanks in advance! Also, sorry in advance if this is in the wrong section.
It is possible. You'd need to have an array of numbers and then roll between 0 and array's length - 1.
Wouldn't doing that be the same as the script above, though?
Let's say I have an array of three - [1,5,10]. Rolling a random number between 1 and 10 gives us many potentially unwanted results - 2,3,4,6,7,8 and 9. So if you want to roll among concrete numbers, you need to put them in an array and then roll a random number between 0 and length - 1.
Could I set that up using the above script, or would it need to be written differently?
As I've said. You can use the above script to find the index of the array, since array indexes are just linear sequences. Nevertheless, you'll have to modify it to supply the array of integers you'd want to pick from afterwards.
How would I go about doing that? Where would I start?
Maybe something like one of these?
JavaScript:
// Set of values to pick from
var choose = [1, 5, 13, 15, 17, 19, 21, 25, 29, 40];

// A) Pick 1 value
var a = choose[Math.randomInt(choose.length)];

// B) Pick 5 values, with replacement (i.e. duplicates possible)
var b = [];
for (var n = 5; n--;)
  b.push(choose[Math.randomInt(choose.length)]);

// C) Pick 5 values, without replacement (i.e. no duplicates)
var c = [];
for (var n = 5; n--;)
  c.push(choose.splice(Math.randomInt(choose.length), 1)[0]);

// Log results to console (testing)
console.log('a:', a, '\nb:', b, '\nc:', c);
I appreciate you trying to help me, and I apologize for being such a bother, but I'm sorry, I'm still not understanding. I am lost here. Now I see how it would work, but what I'm not getting is where to put it or what to do with it. Am I suppose to replace a line or lines in the script with one of those? Am I supposed to add one of those lines to it? Do I replace the entire script? Am I supposed to put the numbers inside the brackets next to var? What about a, b, and c? Do I replace those with the variable number? Where to put or what do I do with the line under Log Results, if anything?

Also, could I just use a Check.is_any script to reroll numbers and do it that way? That's what I'm doing for my randomized chests, but I don't know if that's going to work correctly. I'm honestly about to redo my entire item, weapon and armor lists (and I really don't want to do that because then the lists in game would be all over the place and it would be a pain for the player to look for a certain item and such.)
Ah, OK! So, this is your current script call:
JavaScript:
var id = 26, type = 0, min = 1, max = 20, inv = 10;

var array = [], temp = [], rand;
for(i = 0; i < inv; i++) {
   do { rand = Math.floor(Math.random()*(max-min+1)+min);
   } while (temp.indexOf(rand) > -1);
   temp[i] = rand;
   array[i] = [type, rand, 0];
}
if(Array.isArray($gameVariables.value(id)))
   var array = $gameVariables.value(id).concat(array);
$gameVariables.setValue(id, array);
Instead of selecting rand from min to max, you asked how to select it from a list of possible values, right? (If not, then I've misinterpreted your original question.) I've called that list of values choose in my example.

You can get a random index for an array, arr, by rolling a random integer, rand, from 0 to arr.length-1. Then you can get the value at that index as arr[rand]. I think you know this, I'm just stating it for completeness.

Currently you have a temp array to remember selected values, and a do...while loop to re-roll any conflicting values. This is selection without replacement, i.e. option C from my suggestions. Instead of re-rolling duplicates, my suggestion uses the splice method to remove the selected value from the choose array. Technical details here:
Alternatively you could carry on using your temp/do...while approach, that's OK too.

Script call suggestion:
JavaScript:
var id = 26, type = 0, inv = 5;
var choose = [1, 5, 13, 15, 17, 19, 21, 25, 29, 40];
var array = [], rand = 0;
for (i = 0; i < inv; i++) {
   rand = Math.randomInt(choose.length);
   array[i] = [type, choose.splice(rand, 1)[0], 0];
}
if (Array.isArray($gameVariables.value(id)))
   var array = $gameVariables.value(id).concat(array);
$gameVariables.setValue(id, array);
A couple more notes:
  • I've changed inv = 10 to 5 because your choose example only has 10 possible values. Naturally, you can change all the values as needed~

  • RMMV defines Math.randomInt(max) as a function that gets a random integer from 0 to max - 1, so I've used that because it looks nicer.
Ahh, I see what I messed up on now. I needed to use
JavaScript:
splice
method from your Option C. I was trying to use Option B without the splice method and was getting errors. I knew I had to change that first line somehow and add something under that, but was trying to figure out where to put it exactly. Thanks a bunch caethyril and Poryg for all the help! Much appreciated.
It looks possible. You just need to indicate the range of numbers you want the program to choose from. It looks like you have already written the script for that. You will just need to modify it a little.
You can also read more about real numbers and integers on https://difference-between.net/difference-between-real-numbers-and-integers/ and see which ones it’s better to choose. Maybe you would need to use real numbers instead of integers to make the program choose in a range of 1…+20? It might make a choice more randomized and make the shops it builds much more different.

Original thread: https://forums.rpgmakerweb.com/threads/randomizing-a-shop-inventory-using-different-integers.150069/