Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Creating a Random Number Generator

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 118
    Comment on it

    Use a combination of JavaScript Math methods: random to generate a random value between 0 and 1, which is then multiplied by 255.

    var randomNumber = Math.floor(Math.random() * 255);
    console.log(randomNumber);
    

    The random method generates a random number between 0 and 1. To increase the range, multiply the result by the upper end of the range of values you want. If you need a random number with a higher lower end, such as a number between 5 and 10, multiply the value from random by a number equal to the upper range, minus the lower range, minus 1, and then add the lower range to the result:

    var randomNumber = Math.floor(Math.random() * 6) + 5;
    console.log(randomNumber);
    

    The floor method rounds down the floating-point value to the nearest integer.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: