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

前端 未结 22 2355
攒了一身酷
攒了一身酷 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: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
    

提交回复
热议问题