[Solved]Show Picture From Random Variable No Repeat

I am trying to event a gachapon system. So there is this Draw 10 option. It will show 10 random pictures in a 5 x 2 area. 5 columns 2 rows. When an item is drawn it should not repeat. I know how to do the random pick, but i dont know how to avoid drawing the same thing. I mean if there are 100 items in the pool.. does this mean 100 different switches?
I can't follow you. Can you give some more details? Also I don't know what a Gachapon is.
you can do that by array...
but... you need to learn array first
try searching about array and JavaScript
Let me confirm...
You want to do gacha pool that contain a total of 100 items. Player can pull x10 at a time, and the content will be updated the number of times player pulled the item. You mentioned your problem is showing the pictures which I assumed to be something like this :


Your traditional eventing will have problem to accomplish that. The problem is updating and saving the content of items left. To be able to saving the content into the system without sacrificing much, you have to turn a game variable into array. The rest, including show pictures, will have to involve script call and some calculation.

Sure, I can do a quick example...
let me have dinner first.
If you use a script call to initialize an array:
JavaScript:
$gameVariables.gachapon = []

You can either A add all the items into the pool directly:
JavaScript:
$gameVariables.gachapon = [1, 2, 19, 20, 28, 50]

or B add single item when you want to:
JavaScript:
$gameVariables.gachapon.push(51)


Now you set a game variable to a randomly drawn item from the pool and simultaneously remove it from the pool:
JavaScript:
$gameVariables.setValue(8, $gameVariables.gachapon.splice(Math.floor(Math.random() * $gameVariables.gachapon.length), 1))
I used game variable 8 here in the example but you can change it to whatever you want!


To check if the pool is empty, put this into the conditional branch script box:
JavaScript:
!$gameVariables.gachapon.length
It returns true if the pool is empty!
Gachapon is a random draw from a prize pool. If I draw a prize (with item ID 9) the next item drawn should not be the same. I want a unique draw.

I will check the array.. I think i recall finding unique random generated very time. Thank you for the replies..
So let say the array is pool = [ 1..100]
When i randomize var 1 for slot 1 it will remove value of var 1 from the array pool
then var2 for slot2.. until var 10.. such that all draws are unique..

I don't need to store what's item left.. i just need that during the draw there are no repeats..

I have no problem with the picture random.. i found this code in the forums..
Code:
$gameScreen.showPicture(5, 'prize_' + $gameVariables.value(85), 0, 35, 150, 80, 80, 255, 0);

var reward = $dataItems[$gameVariables.value(85)];
$gameParty.gainItem(reward, 1);

is this correct setup for the array?
Code:
$gameVariables.gachapon = []
for (int i = 0; i < 100; i++) { 
    $gameVariables.gachapon[i]=i;
}
With the foor loop, are you trying to add items 1 through 100 to the pool?
Then you're almost right!

JavaScript:
$gameVariables.gachapon = []
for (i = 0; i < 100; i++) {
    $gameVariables.gachapon[i] = i + 1
}

+ 1 because item ids start at 1, so you don't draw 0 which would give you undefined :')
Edited above answer in case you didn't see...!
I think i have to reorder the process.. so how do i code it correctly?I will randomize 10 variables value then add them to the array.. then draw the values one by one reducing the array size.. I think this will be easier than adding 100 values then assign random values to 10 variables without repeat.
If you use a script call to initialize an array:
JavaScript:
$gameVariables.gachapon = []

You can either A add all the items into the pool directly:
JavaScript:
$gameVariables.gachapon = [1, 2, 19, 20, 28, 50]

or B add single item when you want to:
JavaScript:
$gameVariables.gachapon.push(51)


Now you set a game variable to a randomly drawn item from the pool and simultaneously remove it from the pool:
JavaScript:
$gameVariables.setValue(8, $gameVariables.gachapon.splice(Math.floor(Math.random() * $gameVariables.gachapon.length), 1))
I used game variable 8 here in the example but you can change it to whatever you want!


To check if the pool is empty, put this into the conditional branch script box:
JavaScript:
!$gameVariables.gachapon.length
It returns true if the pool is empty!
Wasn't this very helpful? Should I be more detailed in my explanation maybe?
it is already very helpful. i do not understand the part where i should put this:

$gameVariables.setValue(8, $gameVariables.gachapon.splice(Math.floor(Math.random() * $gameVariables.gachapon.length), 1))

I'm sorry. I can setup the array. but i cant get that part to work.. I'm still getting duplicates..
I see, that script call will set variable 8 to a random item from the pool. When it picks the random item, it also dissapears from the pool.
For example, let's say your pool is [1, 2, 3, 4, 5, 6, 7]
When you call
JavaScript:
$gameVariables.setValue(8, $gameVariables.gachapon.splice(Math.floor(Math.random() * $gameVariables.gachapon.length), 1))
Game variable 8 is set to a randomly picked item, let's say it picks 6. Then Game variable 8 is now 6!
And the pool will now be [1, 2, 3, 4, 5, 7]

Then you can call this:
JavaScript:
$gameScreen.showPicture(5, 'prize_' + $gameVariables.value(8), 0, 35, 150, 80, 80, 255, 0)
$gameParty.gainItem($dataItems[$gameVariables.value(8)], 1)

And the picture that will show is prize_6 and item 6 will be added to the party inventory!


You can call all three together for instant results! So the next time you call
JavaScript:
$gameVariables.setValue(8, $gameVariables.gachapon.splice(Math.floor(Math.random() * $gameVariables.gachapon.length), 1))
$gameScreen.showPicture(5, 'prize_' + $gameVariables.value(8), 0, 35, 150, 80, 80, 255, 0)
$gameParty.gainItem($dataItems[$gameVariables.value(8)], 1)
you can't get 6 anymore because it is no longer in the pool.
Due to the nature of showPicture, you can't show multiple pictures with the same picture id. If you want to show many pictures you have to give a new id to each one. It's the first number in the function, the one that's 5 right before 'prize' in
JavaScript:
$gameScreen.showPicture(5, 'prize_' + $gameVariables.value(8), 0, 35, 150, 80, 80, 255, 0)
//                      ^ that one
So for the second picture, make it 6, for the third make it 7 and so on! But maybe you knew this already! Because you would already change the x and y so they're not on top of each other :q

JavaScript:
$gameScreen.showPicture(pictureId, name, origin, x, y, scaleX, scaleY, opacity, blendMode)
Mmm, if the content doesn't need to be saved, then there's no actual need to use variable. You can just use temporary array.

Also If the value 1..100 refer to the item id, then you've already prepared 100 pictures for each item? 😳 Neat...

Here's the complete solution :
1589116655858.png

* I forgot MV default width and height.
* You may change the value inside the blue marker to any constant to set the margin.
* You may change the value with the red marker to set the distance between picture.
* I made mistake in the screenshot on last line (roll+1). Fixed it in code below.

Here's the code :
Code:
var pool = [];
for (var i=0; i<100; i++) pool.push(i+1);
for (var i=0; i<10; i++){
var roll = Math.randomInt(pool.length);
var xx = ((Graphics.width-500)/2) + (i%5)*100;
var yy = ((Graphics.height-200)/2) + Math.floor(i/5)*100;
$gameScreen.showPicture(1+i, 'itempic_'+roll, 1, xx, yy, 100, 100, 255, 0);
pool.splice(roll, 1);
$gameParty.gainItem($dataItems[roll], 1);
}
I see now.. so if the variable ID is 8, it should be 8 for setvalue, showpicture, and gain item..

thank you @ozubon .. now working properly!

@Kuro DCupu i will try that for the simultaneous appearance thank you! yes, because the bonus plugin Gacha require images i have 100+
I about to suggest the var too
but... I too lazy to do 😂
never understand the math % in the code
so I just observer here hehe...
@pasunna
% aka modulo is like a clock!
If the clock is 9, what is it in 6 hours, 3 right? (9 + 6) % 12 = 3

A number before % will never exceed the number after it. It will add the remainder until there is no remainder.

So 11 % 10 is 1 because at 10 it loops back and 1 is left.
12 % 10 is 2 of course!
22 % 10 is also 2 because it loops two times and 2 is left.
10 % 10 is 0 because it stops just when it has looped back to 0.
9 % 10 is 9 because it's lower than 10 and doesn't loop.
@pasunna
% aka modulo is like a clock!
If the clock is 9, what is it in 6 hours, 3 right? (9 + 6) % 12 = 3

A number before % will never exceed the number after it. It will add the remainder until there is no remainder.

So 11 % 10 is 1 because at 10 it loops back and 1 is left.
12 % 10 is 2 of course!
22 % 10 is also 2 because it loops two times and 2 is left.
10 % 10 is 0 because it stops just when it has looped back to 0.
9 % 10 is 9 because it's lower than 10 and doesn't loop.
oh my god!
I always go
if A > 10
A = 0
😂
this will real helpful thank you!
I will combine them with Moghunter picture effect and TDDP_AnimationCurves for more dramatic. Thank you all.
Kuro's solution is very good because it automates the proccess efficiently. You then don't have to tweak the image id and x and y and all that jazz over and over! It's true that you don't need to use saveable variables for the pool unless the player can save between the pool is filled and empty.

@Kuro DCupu a little splice trick if you want; splice returns whatever it splices so therefore this:
JavaScript:
pool.splice(roll, 1);
$gameParty.gainItem($dataItems[roll], 1);
could be truncated to this
JavaScript:
$gameParty.gainItem($dataItems[pool.splice(roll, 1)], 1);
and it would have the same result!

Original thread: https://forums.rpgmakerweb.com/threads/solved-show-picture-from-random-variable-no-repeat.121347/