Generate random number between two numbers in JavaScript

后端 未结 23 2331
走了就别回头了
走了就别回头了 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条回答
  •  萌比男神i
    2020-11-22 01:22

    This is about nine years late, but randojs.com makes this a simple one-liner:

    rando(1, 6)
    

    You just need to add this to the head of your html document, and you can do pretty much whatever you want with randomness easily. Random values from arrays, random jquery elements, random properties from objects, and even preventing repetitions if needed.

    
    

提交回复
热议问题