Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: random number

  1. #1
    New to the CF scene
    Join Date
    Jul 2002
    Location
    pittsburgh, pa
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    random number

    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

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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;


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •