View Full Version : random number

xerus

07-25-2002, 01:46 AM

I'm just learning to program in javascript and I know how to generate a random number using Math.random(). I need to generate a random even number in the range 2 to 10. Any suggestions? I've looked through numerous texts and websites and can't find anything.

Sam

brothercake

07-25-2002, 01:51 AM

Well,

rnd = Math.random() * 10;

gives you a number between 0 and 10. You can round it off with Math.round() :

rnd = Math.round(Math.random() * 10);

so you have an integer between 0 and 10. So to get a number between 2 and 10, you could make one between 0 and 8, then add 2:

rnd = Math.round(Math.random() * 8) + 2;

Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.