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 Profile Posts

Both the polls for the 3rd Original Character Contest ended with 5th place ties! So let's break those ties. Vote now for your favorite of the ones tied!


werewolf and his friends added!!

YAY! I just posted my third Guide! Setting up STEAM Integration in RPG Maker MV WITH updated NW.js (huge performance update)
rux
MUCLSES!!! :epicface: :epicface:
If you're reviewing someone else's game, don't focus only on the negative and criticize them. Use positive constructive feedback. As game developers, we need to empower one another; not put each other down.

Forum statistics

Threads
107,503
Messages
1,030,122
Members
139,631
Latest member
tealearl
Top