Indexing random items to be sold at a shop

I was wondering if it is possible to have a items from a list to be added to the shop at random?

For now I have a list of 16 items and I would like to have 8 of those items to be randomized into a shop. Is it possible to do something like a random variable to loop for 8 times and add an item depending on its outcome? Either by a plugin or script.

Thanks
I Googled "rpg maker mv random shop":

I tried using the script from the second link and this is how my event is set up:

Spoiler: Event
1717434872582.png

But whenever I interact with the event in-game I get this error:
Spoiler: TypeError Message
1717434945309.png

Any clue why that's happening?
Here's a neater code, but ONLY if the item is all from "item" type and the price doesn't change.
JavaScript:
var list = [list_of_item_ids];
var goods = [];
for(var i=0; i<8; i++){
 goods.push([0, list.splice(Math.randomInt(list.length),1), 0]);
}
SceneManager.push(Scene_Shop);
SceneManager.prepareNextScene(goods, true);

Here's a sample of 8 out of 16 item randomized to a shop:
1717437546959.png


The result:
1717437605826.png
In case it helps, here's the basic syntax from the MV / MZ Script Call List:

scl.png
I recommend Ethernite's suggestion: it's much neater than the one you quoted. 👋

The problem was that you were missing the last line in your first Script command:
JavaScript:
$gameVariables.setValue(id, array);
That stores array in a game variable. That variable is used to set up the shop goods in the second Script command, so it's pretty important to put the array in there.
Sweet! Both methods work now, however they randomize a new list every time I interact with the shop. Is there a way that I can keep the first randomized list?

The game works like a roguelike where you return to the shop after a battle, the idea is to have a new randomized list after each battle!
You'd save goods into a game variable, using the syntax @caethyril demonstrated:
Code:
$gameVariables.setValue(42, goods);

Then you can do SceneManager.prepareNextScene($gameVariables.value(42), true);

And it will be the same shop list until you re-run the code that @Ethernite gave you and re-store goods into the game variable.
So I tried that on both cases and I still have the same issue... I'm not sure what I could be missing.

Here's what it looks using the script from the link:
Spoiler: Link script
1717448800346.png

And here is what it looks using @Ethernite 's script:

Spoiler: Ethernite script
1717448864264.png
You still have it all in the same event set to action button, which is presumably where your shop is.

So it's still executing all of the randomizing code every time you press for the shop. Just using a variable at the bottom doesn't change anything if you have all of the preceding lines of code still there.

Logically, if you want the randomizing to not happen every time, you need to separate those things - that's why I told you how to separate them using a game variable. That's like saying "You said I can take a shortcut by taking a left, but I'm still going straight to the traffic light - why isn't my trip shorter?" 😉

So you have one common event or something with all of the code before SceneManager.push() and you insert the saving to the variable that I showed you.

Code:
var list = [list_of_item_ids];
var goods = [];
for(var i=0; i<8; i++){
goods.push([0, list.splice(Math.randomInt(list.length),1), 0]);
}
$gameVariables.setValue(33, goods);

And that randomizes the goods and saves them in a variable.

Then the event you trigger to start the shop just has
Code:
SceneManager.push(Scene_Shop);
SceneManager.prepareNextScene($gameVariables.value(33), true);

And you don't call your randomizing event again until whatever point in your game you want to re-randomize it.
I was playing around with this by splitting them into two different pages with each half of the script and still had issues.

I put a self-switch to randomize it once, but it doesnt randomize after I'm done with a fight.

Spoiler: Common Event
1717450794210.png

Spoiler: Event Page 1
1717450827061.png

Spoiler: Event Page 2
1717450856895.png

In the enemy event I have a line to turn the SHOP switch off after the battle is over, but it doesnt run the randomization again
In the enemy event I have a line to turn the SHOP switch off after the battle is over, but it doesnt run the randomization again

I see, so you planned for the shop to be randomized only every after a battle.

First, make sure that autorun event is on the same map as your character to be properly triggered.
Second, you misplaced the bracket for the code on that second page.
The second line of the code should be:
SceneManager.prepareNextScene($gameVariables.value(33), true);
The player starts in a hub room where the shop is located, but the battles happen in a separate room.

It's strange because the battles are also randomized and it works just fine.

Second, you misplaced the bracket for the code on that second page.
The second line of the code should be:
SceneManager.prepareNextScene($gameVariables.value(33), true);
Thanks for pointing this out, I get easily lost with typos in code. However, even with this fix it still keeps the first item list in the shop after the battles

EDIT: Nevermind, I realized the SHOP = OFF was in the wrong enemy event 😅

Thank you all for the support 😍

Actually one more question, how would I enable selling items to this shop?
Code:
SceneManager.push(Scene_Shop);
SceneManager.prepareNextScene($gameVariables.value(33), false);

Original thread: https://forums.rpgmakerweb.com/threads/indexing-random-items-to-be-sold-at-a-shop.169222/