Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Math.random()

Status
Not open for further replies.

Sam63

Technical User
Nov 19, 2001
18
US
I am familar with using Math.random()...

i.e.
var i = Math.floor(1 + Math.random() * 10);

but I'm not sure how to get a random number in a set of numbers i.e. 2, 4, 6, 8, 10 ....any suggestions.

Sam
 
Sam63,

if memory serves correctly, Math.Floor(Math.random() * x) will return a number between 0 and x.

======================================

if (!succeed) try();
-jeff
 
I need to get a random number from a set of numbers that may not be consecutive, i.e.

6, 10, 14, 18 or 2, 4, 6, 8, 10

Math.floor(Math.random * x) does not do this. Any suggestions??
 
hmm...

set up an array to hold your set of numbers:

var set = new Array('2','4','6','8','10');

then use Math.floor(Math.random * set.length);

hope this works...I can't test it on the public computer I'm at right now... :-( ======================================

if (!succeed) try();
-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top