Random item from array

jlusco

Veteran
Veteran
Joined
May 29, 2016
Messages
33
Reaction score
63
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
63
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
63
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:
 

Latest Threads

Latest Profile Posts

"Man is made by his belief. As he believes, so he is."-Krishna
FastFood in my country is neither fast nor cheap like it's expected to be, but people still buy a lot of it. I don't want to imagine how much of it people would eat if it was.
Started finishing up my bosses and characters...I don't know how long...but its going to take long...

I ended up needing to do a chapter one world map remake. It's still WIP but coming along nicely. =D
Stream will be live shortly! I will be beginning a play-through of Resident Evil 4! Feel free to drop by!

Forum statistics

Threads
107,753
Messages
1,031,900
Members
139,898
Latest member
Karuu
Top