How to Generate a random number of fixed length using JavaScript?

前端 未结 22 2348
攒了一身酷
攒了一身酷 2021-01-30 10:23

I\'m trying to generate a random number that must have a fixed length of exactly 6 digits.

I don\'t know if JavaScript has given below would ever create a number less th

相关标签:
22条回答
  • 2021-01-30 10:27

    Based on link you've provided, right answer should be

    Math.floor(Math.random()*899999+100000);

    Math.random() returns float between 0 and 1, so minimum number will be 100000, max - 999999. Exactly 6 digits, as you wanted :)

    0 讨论(0)
  • 2021-01-30 10:29

    This code provides nearly full randomness:

    function generator() {
        const ran = () => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0].sort((x, z) => {
            ren = Math.random();
            if (ren == 0.5) return 0;
            return ren > 0.5 ? 1 : -1
        })
        return Array(6).fill(null).map(x => ran()[(Math.random() * 9).toFixed()]).join('')
    }
    
    console.log(generator())

    This code provides complete randomness:

    function generator() {
    
        const ran1 = () => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0].sort((x, z) => {
            ren = Math.random();
            if (ren == 0.5) return 0;
            return ren > 0.5 ? 1 : -1
        })
        const ran2 = () => ran1().sort((x, z) => {
            ren = Math.random();
            if (ren == 0.5) return 0;
            return ren > 0.5 ? 1 : -1
        })
    
        return Array(6).fill(null).map(x => ran2()[(Math.random() * 9).toFixed()]).join('')
    }
    
    console.log(generator())

    0 讨论(0)
  • 2021-01-30 10:29

    You can use the below code to generate a random number that will always be 6 digits:

    Math.random().toString().substr(2, 6)
    

    Hope this works for everyone :)

    Briefly how this works is Math.random() generates a random number between 0 and 1 which we convert to a string and using .toString() and take a 6 digit sample from said string using .substr() with the parameters 2, 6 to start the sample from the 2nd char and continue it for 6 characters.

    This can be used for any length number.

    If you want to do more reading on this here are some links to the docs to save you some googling:

    Math.random(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random

    .toString(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString

    .substr(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr

    0 讨论(0)
  • 2021-01-30 10:30

    Only fully reliable answer that offers full randomness, without loss. The other ones prior to this answer all looses out depending on how many characters you want. The more you want, the more they lose randomness.

    They achieve it by limiting the amount of numbers possible preceding the fixed length.

    So for instance, a random number of fixed length 2 would be 10 - 99. For 3, 100 - 999. For 4, 1000 - 9999. For 5 10000 - 99999 and so on. As can be seen by the pattern, it suggests 10% loss of randomness because numbers prior to that are not possible. Why?

    For really large numbers ( 18, 24, 48 ) 10% is still a lot of numbers to loose out on.

    function generate(n) {
            var add = 1, max = 12 - add;   // 12 is the min safe number Math.random() can generate without it starting to pad the end with zeros.   
    
            if ( n > max ) {
                    return generate(max) + generate(n - max);
            }
    
            max        = Math.pow(10, n+add);
            var min    = max/10; // Math.pow(10, n) basically
            var number = Math.floor( Math.random() * (max - min + 1) ) + min;
    
            return ("" + number).substring(add); 
    }
    

    The generator allows for ~infinite length without lossy precision and with minimal performance cost.

    Example:

    generate(2)
    "03"
    generate(2)
    "72"
    generate(2)
    "20"
    generate(3)
    "301"
    generate(3)
    "436"
    generate(3)
    "015"
    

    As you can see, even the zero are included initially which is an additional 10% loss just that, besides the fact that numbers prior to 10^n are not possible.

    That's now a total of 20%.

    Also, the other options have an upper limit on how many characters you can actually generate.

    Example with cost:

    var start = new Date(); var num = generate(1000); console.log('Time: ', new Date() - start, 'ms for', num)
    

    Logs:

    Time: 0 ms for 7884381040581542028523049580942716270617684062141718855897876833390671831652069714762698108211737288889182869856548142946579393971303478191296939612816492205372814129483213770914444439430297923875275475120712223308258993696422444618241506074080831777597175223850085606310877065533844577763231043780302367695330451000357920496047212646138908106805663879875404784849990477942580056343258756712280958474020627842245866908290819748829427029211991533809630060693336825924167793796369987750553539230834216505824880709596544701685608502486365633618424746636614437646240783649056696052311741095247677377387232206206230001648953246132624571185908487227730250573902216708727944082363775298758556612347564746106354407311558683595834088577220946790036272364740219788470832285646664462382109714500242379237782088931632873392735450875490295512846026376692233811845787949465417190308589695423418373731970944293954443996348633968914665773009376928939207861596826457540403314327582156399232931348229798533882278769760
    

    More hardcore:

    generate(100000).length === 100000 -> true
    
    0 讨论(0)
  • 2021-01-30 10:31

    console.log(Math.floor(100000 + Math.random() * 900000));

    Will always create a number of 6 digits and it ensures the first digit will never be 0. The code in your question will create a number of less than 6 digits.

    0 讨论(0)
  • 2021-01-30 10:31
    100000 + Math.floor(Math.random() * 900000);
    

    will give a number from 100000 to 999999 (inclusive).

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