How to Random number JavaScript

How to create a function to get a random number.

Math.random

We will use the Math.random function.

Example

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 Rick

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.

Leave a Reply

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

*
*