How to Random number JavaScript

How to create a function to get a random number.


We will use the Math.random function.


Math.random()*3 will return 0, 1 and 2.

var players = Array();

players[0] = "Max";
players[1] = "Ted";
players[2] = "Marshall";

var randomnumber=Math.floor(Math.random()*3)

alert(players[randomnumber] + "!");

About Ricard Torres

Senior Front-end Software Engineer from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.

@ricard_dev @ricard_dev

📝 Blog 🎙 Podcast

Leave a Reply

Add <code> Some Code </code> by using this tags.