1

固定長の nonce (長さ 9) を生成しようとしています。しかし、私のコードは、長さ 8 の nonce と 9 の長さの nonce を出力することがあります。

これは私がやろうとしていることですが、アプローチが異なります (固定ノンス長に変更しました)

長さを 9 として引数として渡しているときに、なぜ長さ 8 の nonce を出力するのか理解できませんか??

誰かがなぜこれが起こっているのかを教えてくれれば素晴らしいでしょう。

以下は完全なNodejsコードです

var last_nonce = null;
var nonce_incr = null;
// if you call new Date to fast it will generate
// the same ms, helper to make sure the nonce is
// truly unique (supports up to 999 calls per ms).
module.exports = {
    getNonce: function(length) {
        if (length === undefined || !length) {
            length = 8;
        }
        var MOD = Math.pow(10, length);
        var now = (+new Date());
        if (now !== last_nonce) {
            nonce_incr = -1;
        }
        nonce_incr++;
        last_nonce = now;
        var nonce_multiplier = ((nonce_incr < 10) ? 10 : ((nonce_incr < 100) ? 100 : 1000));
        var s = (((now % MOD) * nonce_multiplier) + nonce_incr) % MOD;
        return s;
    }
}
//test code
if(require.main === module) {
    console.time("run time");
    //importing async module
    var async = require('async');
    var arr = [];
    //generating 1000 length array to use it in making 1000 async calls
    //to getNonce function
    for(var i=0; i<1000; i++) arr.push(i);
    //this will call getNonce function 1000 time parallely
    async.eachLimit(arr, 1000, function(item, cb) {
        console.log(module.exports.getNonce(9));
        cb();
    }, function(err) {console.timeEnd("run time");});
}

出力例:

708201864  --> nonce length 9
708201865
708201866
70820190   --> nonce length 8 (why it is coming 8?? when passed length is 9)
70820191
70820192
70820193
70820194
70820195
70820196
70820197
70820198
70820199
708201910
708201911
708201912
708201913
708201914
708201915
708201916
708201917
708201918
4

0 に答える 0