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:
 

Latest Threads

Latest Posts

Latest Profile Posts

Ami


Imagine you (as Miner) Sleep in the Cave,the other side is a Bunch of Golds,how d'you feel?
finally got footage of my main character running around his corrupted street...
I finally got the ships and water to flow like I wanted to~ the canals are a success. Now to loop the animation for the kites. So many kites.
Bendyizzt wrote on staggqueen's profile.
Hello Newcomer...

Forum statistics

Threads
107,694
Messages
1,031,493
Members
139,819
Latest member
KirbDog
Top