Random item from array

jlusco

Veteran
Veteran
Joined
May 29, 2016
Messages
33
Reaction score
62
First Language
Eng
Primarily Uses
So I have set an array
var items =[1,2,3,4,5];
How do I then set a variable to be a random value from the array?
Sorry I suck as javascript
 

KenSoulslayer

Graphic Designer/JS Dev
Member
Joined
Nov 29, 2020
Messages
26
Reaction score
10
First Language
Hindi
Primarily Uses
RMMZ
Code:
const randomEl = items[Math.floor(Math.random() * items.length)];
If you need me to explain this snippet, ask and I will let you know.

Also using let is better in JS if you are not going to use this variable in another code snipper as var has global scope and mess up other variables, if you are not sure which one to use and you dont have a lot of variables var is fine.
If the value wont change during runtime just use const
 

jlusco

Veteran
Veteran
Joined
May 29, 2016
Messages
33
Reaction score
62
First Language
Eng
Primarily Uses
Code:
const randomEl = items[Math.floor(Math.random() * items.length)];
If you need me to explain this snippet, ask and I will let you know.

Also using let is better in JS if you are not going to use this variable in another code snipper as var has global scope and mess up other variables, if you are not sure which one to use and you dont have a lot of variables var is fine.
If the value wont change during runtime just use const
So if I understand you I would do
let items =[1,2,3,4,5];
const randomEl = items[Math.floor(Math.random() * items.length)];
$gameParty.gainItem($dataItems[randomEl],1);
to reward the random item from the list. Am I understanding this correctly?
 

jlusco

Veteran
Veteran
Joined
May 29, 2016
Messages
33
Reaction score
62
First Language
Eng
Primarily Uses
Code:
const randomEl = items[Math.floor(Math.random() * items.length)];
If you need me to explain this snippet, ask and I will let you know.

Also using let is better in JS if you are not going to use this variable in another code snipper as var has global scope and mess up other variables, if you are not sure which one to use and you dont have a lot of variables var is fine.
If the value wont change during runtime just use const
You are a life saver this worked perfectly for the random reward I was working on. Thank you.
 

KenSoulslayer

Graphic Designer/JS Dev
Member
Joined
Nov 29, 2020
Messages
26
Reaction score
10
First Language
Hindi
Primarily Uses
RMMZ
So if I understand you I would do
let items =[1,2,3,4,5];
const randomEl = items[Math.floor(Math.random() * items.length)];
$gameParty.gainItem($dataItems[randomEl],1);
to reward the random item from the list. Am I understanding this correctly?
Yes that's right, for brevity's sake you can also use it in a single line and avoid declaring the random variable as below!

Code:
let items =[1,2,3,4,5];
$gameParty.gainItem($dataItems[items[Math.floor(Math.random() * items.length)]],1);
and such won't really affect anything though

You are a life saver this worked perfectly for the random reward I was working on. Thank you.
You're welcome, glad I could help! :kaopride:
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Who would think putting a power button on a side of a laptop is a good thing? You want to move the laptop and you suddenly press the button.
The second draft of my script is almost done, and I don't think I'm going to write a third draft. I'll make changes to dialogue here and there, but I'm happy with the series of events the way they are.
How you feel about font and dialogue box changing for every main character while the minor cast and regular npc get their own styles? Too much?
Just found an obviously custom effect (i.e. edited by me) in my resources>sounds folder called "rip jaw off". Dear Lord what is wrong with me as a person that at some point I needed a sound *specifically* for THAT??
(...rhetorical question.)

Forum statistics

Threads
107,526
Messages
1,030,282
Members
139,651
Latest member
jasasewamobilbali
Top