random item with no repeats?

hey there! ive found a way to give the player 5 random items by calling this common event five times:
JavaScript:
var temp = [];
$dataItems.forEach(function(v) {if (v && v.name) {temp.push(v);}});
$gameParty.gainItem(temp[Math.floor(Math.random()*temp.length)],1);
but i dont want any multiples of items just 5 different items each time- was wondering if theres a simple way accomplish this?
1626548822182.png
Havent tested, but it should work:

JavaScript:
var temp = [], used = [];
$dataItems.forEach(function(v) {if (v && v.name) {temp.push(v);}});
for(var i = 0; i < 5; i++){
    var id = Math.floor(Math.random()*temp.length);
    if(!used.includes(id)){
        $gameParty.gainItem(temp[id],1);
        used.push(id);
    }else{
        i--;
    }
}

So you have to run the event only once.
This might be more efficient:
JavaScript:
var items = $dataItems.filter(function(item) {
  return item && item.name;
});
for (var n = 0; n < 5; n++) {
  if (items.length) {
    var r = Math.randomInt(items.length);
    $gameParty.gainItem(items.splice(r, 1)[0], 1);
  }
}
I.e. remove the selected item from the list each time.

You can find technical information about the filter and splice methods here:
oh wow!! thanks so much you guys really appreciate it!
i found a work-around too while i was working on it
which was the "ctb_randomloot" plugin
with these script calls for anyone who wants to know:
1626556023267.png

(5 sets of 6 items to alternate from per 5 pictures in total 30 items)
this can be closed now! :0)
Thread closed

Original thread: https://forums.rpgmakerweb.com/threads/random-item-with-no-repeats.138618/