Generate random number between two numbers in JavaScript

后端 未结 23 2093
走了就别回头了
走了就别回头了 2020-11-22 01:09

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?

23条回答
  •  太阳男子
    2020-11-22 01:40

    Example

    Return a random number between 1 and 10:

    Math.floor((Math.random() * 10) + 1);
    

    The result could be: 3

    Try yourself: here

    --

    or using lodash / undescore:

    _.random(min, max)

    Docs: - lodash - undescore

提交回复
热议问题