Generate random number between two numbers in JavaScript

后端 未结 23 2046
走了就别回头了
走了就别回头了 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:21
    var x = 6; // can be any number
    var rand = Math.floor(Math.random()*x) + 1;
    
    0 讨论(0)
  • 2020-11-22 01:22

    I wrote more flexible function which can give you random number but not only integer.

    function rand(min,max,interval)
    {
        if (typeof(interval)==='undefined') interval = 1;
        var r = Math.floor(Math.random()*(max-min+interval)/interval);
        return r*interval+min;
    }
    
    var a = rand(0,10); //can be 0, 1, 2 (...) 9, 10
    var b = rand(4,6,0.1); //can be 4.0, 4.1, 4.2 (...) 5.9, 6.0
    

    Fixed version.

    0 讨论(0)
  • 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;
        }   
    
    0 讨论(0)
  • 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.

    <script src="https://randojs.com/1.0.0.js"></script>
    
    0 讨论(0)
  • 2020-11-22 01:24

    Math is not my strong point, but I've been working on a project where I needed to generate a lot of random numbers between both positive and negative.

    function randomBetween(min, max) {
        if (min < 0) {
            return min + Math.random() * (Math.abs(min)+max);
        }else {
            return min + Math.random() * max;
        }
    }
    

    E.g

    randomBetween(-10,15)//or..
    randomBetween(10,20)//or...
    randomBetween(-200,-100)
    

    Of course, you can also add some validation to make sure you don't do this with anything other than numbers. Also make sure that min is always less than or equal to max.

    0 讨论(0)
  • 2020-11-22 01:26

    Math.random()

    Returns an integer random number between min (included) and max (included):

    function randomInteger(min, max) {
      return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    

    Or any random number between min (included) and max (not included):

    function randomNumber(min, max) {
      return Math.random() * (max - min) + min;
    }
    

    Useful examples (integers):

    // 0 -> 10
    Math.floor(Math.random() * 11);
    
    // 1 -> 10
    Math.floor(Math.random() * 10) + 1;
    
    // 5 -> 20
    Math.floor(Math.random() * 16) + 5;
    
    // -10 -> (-2)
    Math.floor(Math.random() * 9) - 10;
    

    ** And always nice to be reminded (Mozilla):

    Math.random() does not provide cryptographically secure random numbers. Do not use them for anything related to security. Use the Web Crypto API instead, and more precisely the window.crypto.getRandomValues() method.

    0 讨论(0)
提交回复
热议问题