The Java Script Math object's random method returns a floating-point value between 0 and 1. To generate a random integer between zero and n, use the formula provided here.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site
   WARNING!
What you learn from these Totally FREE
Einstein Newsletters could cause your friends to mistake you for someone else!
  [] automobiles
  [] business
  [] parenting
  [] computers
  [] contests
  [] education
  [] entertainment
  [] food/wine
  [] free stuff
  [] genealogy
  [] health/fitness
  [] home/garden
  [] humor
  [] marketing
  [] investing
  [] pets
  [] inspiration
  [] self-improve
  [] recreation
  [] travel
  [] womens stuff
  [] writing/reading
Click here and choose as many as you like!

Generating Random Numbers

The Math.random method returns a floating-point value between 0 and 1. To generate a random integer between zero and n, use the formula below:

var num = Math.floor(Math.random() * n);

To generate a random integer between m and n, use the formula below:

var num = Math.floor(Math.random() * (n - 1)) + m;

For example, if you wanted to design a card game you would need random integers between 1 and 52. These random numbers could be generated with the line shown below.

var card = Math.floor(Math.random() * 51) + 1;

The example below places the names of the cards in an array and then generates a random integer between 1 and 52, which it uses as an index into the array to display the name of a card.

var arCard = new Array(
"A club","2 club","3 club","4 club","5 club","6 club","7 club","8 club","9 club","10 club","J club","Q club","K club",
"A diam","2 diam","3 diam","4 diam","5 diam","6 diam","7 diam","8 diam","9 diam","10 diam","J diam","Q diam","K diam",
"A heart","2 heart","3 heart","4 heart","5 heart","6 heart","7 heart","8 heart","9 heart","10 heart","J heart","Q heart","K heart",
"A spade","2 spade","3 spade","4 spade","5 spade","6 spade","7 spade","8 spade","9 spade","10 spade","J spade","Q spade","K spade");
var card = Math.floor(Math.random() * 52) + 1;
document.write(arCard[card]);

You might use a for loop, as shown below, to "draw" a random five card poker hand. Each time the webpage is loaded, a random poker hand will be displayed.

var card;
for(var i = 1; i < 6; i++)
{
  card = Math.floor(Math.random() * 52) + 1;
  document.write(arCard[card] + " - ");
}

The example below places the file names for six die images into an array. Each time the webpage is loaded, a random pair of die images will be displayed.

var arDie = new Array("1.gif","2.gif","3.gif","4.gif",
"5.gif","6.gif");
var roll;

for(var i = 1; i < 3; i++)
{
  roll = Math.floor(Math.random() * 5) + 1;
  document.write("<img src='" + arDie[roll] + "'>");
}

Random numbers have many uses, from the creation of passwords, generation of abstract art, generation of artificial worlds, to games such as dice and cards. The Java Script Math object's random method returns a floating-point value between 0 and 1. To generate a random integer between zero and n, use the formula provided here.

Web Design Sections

RSS Feed RSS Feed

Java Script Quick Reference
Java Script Data Types
Java Script Reserved Words
Java Script Arithmetic Operators
Comparison Operators
Java Script Arrays
Java Script Character Encoding and Decoding
The if/else Structure
The switch/case Structure
The for Loop
The while Loop
The break Statement
The continue Statement
JavaScript Math Object
Round a Number
Determine Absolute Value
Determine Minimum and Maximum
Generating Random Numbers
Java Script Trigonometric Methods
Java Script Number Object
Format a Number as Currency
Java Script Strings
Compare Two Strings
Find a Character or a Substring Within a String
Include a Quote Character in a String
Include a Backslash Character in a String
Define Lines in a String
Use Escape to Replace Dangerous Characters
Convert a Number to a String
Convert a String to a Number
The Document Object Model (DOM)
Accessing Web Page Elements
Interactively Set Webpage Colors
Get Webpage File Date and File Size
Dueling Windows
Cookie Power Made Easy

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269