Generate random number between two numbers in JavaScript

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

Is there a way to generate a random number in a specified range (e.g. from 1 to 6: 1, 2, 3, 4, 5, or 6) in JavaScript?

Answers

If you wanted to get between 1 and 6, you would calculate:

Math.floor(Math.random() * 6) + 1  

Where:

    • 1 is the start number
    • 6 is the number of possible results (1 + start (6) - end (1) )

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/4959975/generate-random-number-between-two-numbers-in-javascript

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils