Exclude some numbers from random number variable

Ina00

Regular
Regular
Joined
Oct 10, 2020
Messages
147
Reaction score
15
First Language
Italian
Primarily Uses
RMVXA
Let's say for example that I assign to a variable a random number from 1 to 10.
I'd like the chosen number to being removed from the count next time I assign a random number to that variable. (I'll accept any method do to this, even multiple event pages).

Example:
Variable = random 1-10 => Variable = 3

3 is removed from the count, so next time i assign a random number, "3" will never be chosen.
 

Andar

Regular
Regular
Joined
Mar 5, 2013
Messages
39,954
Reaction score
11,895
First Language
German
Primarily Uses
RMMV
the only realistic way to do this is by using an array with elements, and that cannot be done by pure event commands.

basically you start by creating an array of ten elements, give each element one of the numbers from 1 to 10. then you get a random index number, remove the element of that index and use the number inside that element as the value.

Ruby/RGSS3 has functions for array elements, but I don't knoe their syntax, you'll have to search that for yourself.
 

BenSD

Could be anybody
Regular
Joined
Dec 22, 2021
Messages
437
Reaction score
626
First Language
English
Primarily Uses
RMMZ
There's not a way to event this directly, but there are ways to event around this.

Try sticking it in a loop and using switches to mark which numbers have already come up -- if the random number happens to be one that's been used (switched on), then it can just repeat the loop until it gets to one that isn't. If that doesn't make sense, I'm sorry and can post an example.
 

Andar

Regular
Regular
Joined
Mar 5, 2013
Messages
39,954
Reaction score
11,895
First Language
German
Primarily Uses
RMMV
@BenSD that is a rather bad methology because it creates a lot of lag, especially with the last few numbers.

just imagine: you have already randomly rolled 19 of 20 numbers, and now have to loop around until the random function actually hits the last of the twenty numbers.
your method only works with a low number of elements and becomes extremely bad the more element are in the process.
 

Roninator2

Gamer
Regular
Joined
May 22, 2016
Messages
5,256
Reaction score
1,586
First Language
English
Primarily Uses
RMVXA
That's a good point Andar, I was thinking of doing random them save number picked into a second variable and check if it was picked but removing the numbers to pick from like was asked makes more sense. This is my thought so far, but I need to test it. Works
This could be done with script commands in the event.
Set a variable to the numbers you want
$game_variables[xx] = [1,2,3,4,5,6,7,8,9,10]
get random number
$game_variables[xx1] = rand[$game_variables[xx].size)
get number from array
$game_variables[xx2] = $game_variables[xx][$game_variables[xx1]]
remove number from array
$game_variables[xx].delete_at($game_variables[xx1])
1652355406818.png
 
Last edited:

gstv87

Regular
Regular
Joined
Oct 20, 2015
Messages
3,369
Reaction score
2,577
First Language
Spanish
Primarily Uses
RMVXA
Code:
ary = [1,3,6,4]
loop
var = rand(10) + 1
while ary.includes?(var)

ish?
I'm a bit rusty in plain code.
 

Lord Vectra

Master Eventer
Regular
Joined
Dec 6, 2015
Messages
341
Reaction score
423
First Language
English
Primarily Uses
RMVXA
So, I just opened up my VX Ace, and I did what BenSD suggested using Andar's numbers (1-20) while I had stuff moving in the background (to detect any lag).

Now, I didn't use loop because I think loop is bad. I used Label and Jump to Label because that seems to work when Loop doesn't. From testing it, the only lag that pops up is when you have all numbers excluded so the game just freezes as its stuck at getting a random number... forever. Even if all but the number 20 is excluded, you should see no lag. The Makers are pretty quick at proccessing. I did Label Redo at the top, the random number right below, and then I checked for each number the variable could've been and to make it easy, I made the Switch ID the same so if it hits 6, Switch will turn on. If said switch is already on, it'll just to Label Redo which is at the top, so it'll never finish until it hits a number it hasn't already hit.

unknown.png
 

Ina00

Regular
Regular
Joined
Oct 10, 2020
Messages
147
Reaction score
15
First Language
Italian
Primarily Uses
RMVXA
I really thank you all so much for helping!!
I used @Roninator2 's method and it perfectly works.

1652357861453.png
I created the array outside of the event because I thought it would reset with the interaction.


Again, a big thank to you all : D
 

Roninator2

Gamer
Regular
Joined
May 22, 2016
Messages
5,256
Reaction score
1,586
First Language
English
Primarily Uses
RMVXA
I created the array outside of the event because I thought it would reset with the interaction.
The way I showed it, it would. Glad you figured that out.
Happy you used my method. Doesn't happen often that I have a simpler or better way than others.
Maybe I'm getting better/smarter.
Happy game making
 

Ina00

Regular
Regular
Joined
Oct 10, 2020
Messages
147
Reaction score
15
First Language
Italian
Primarily Uses
RMVXA
The way I showed it, it would. Glad you figured that out.
Happy you used my method. Doesn't happen often that I have a simpler or better way than others.
Maybe I'm getting better/smarter.
Happy game making
Thank you <3
 

Latest Threads

Latest Posts

Latest Profile Posts

The Open Hand scandal got even more spicy. The Completionist streamer Jirard made a video response and tossed a bunch of links and documents, but apparently none of it actually addressed why donations he collected sat for 10 years (way too long to justify). He also seemed to threaten defamation suits vs. people for what amounts to pointing out publicly available records. The IRS should definitely audit.
My next update is going to amazing! There's been major additions and things I've wanted to do in previous versions that weren't possible until now. It's not ready yet, but will be soon!
made an RTP edit to try to save on work, found out I was editing the wrong tilesheet of cliffs.
It's fine... I'm fine... I'm sure fixing this won't take too long
Proud of the work done.
Probably gonna toss out some cameos.
-Message sponsored by the β Gang.

Forum statistics

Threads
136,904
Messages
1,271,300
Members
180,691
Latest member
Xhavyedge
Top