Generate random number between two numbers in JavaScript

后端 未结 23 2060
走了就别回头了
走了就别回头了 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
    慢半拍i (楼主)
    2020-11-22 01:22

    I was searching random number generator written in TypeScript and I have written this after reading all of the answers, hope It would work for TypeScript coders.

        Rand(min: number, max: number): number {
            return (Math.random() * (max - min + 1) | 0) + min;
        }   
    

提交回复
热议问题