搜索

首页  >  问答  >  正文

javascript - js生成随机数据

初涉 Js,想生成一组随机数据,基本的数据格式如下:

 var test_data = {
    "data": [{
        "seq": 36687,
        "vol": [63.27994918823242, 63.282066345214844, 63.28112030029297, 63.27827453613281, 63.27593231201172],
        "time": "2012-07-18-21-42-13-034",
        "hrate": 0,
        "status": 0
    },{
        "seq": 36688,
        "vol": [63.24958038330078, 63.249080657958984, 63.25041580200195, 63.25270080566406, 63.254817962646484],
        "time": "2012-07-18-21-42-13-072",
        "hrate": 0,
        "status": 0
    }],
}; 

问题: 我想随机生成 test_data,大概10万行, 纯js代码, seq是自增1,vol为5个随机数字,hrate和status均为0不变,time则是最后3个数字随机变化 (例子中为 42-13-034随机变化)

谢谢!!

巴扎黑巴扎黑2830 天前845

全部回复(3)我来回复

  • 怪我咯

    怪我咯2017-04-11 12:16:21

    <script>
     var test_data = { data: [] };
     var data = test_data.data;
     for(var i = 36687; i < 36687 + 100000; i++){
        var temp = {
            seq: i,
            vol: getVol(),
            time: "2012-07-18-21-42-13-0" + getNum(),
            hrate: 0,
            status: 0
        }
        data.push(temp)
     }
     function getNum(){
      var num = parseInt(Math.random()*100)
      if(num < 10){
        num = "0" + num;
      }
      return num.toString();
     }
     function getVol(){
       var arr = [], i = 0;
       while(i < 5){
        arr.push(Math.random()*100);
        i++;
       }
       return arr;
     }
    </script>

    循环即可.

    回复
    0
  • 大家讲道理

    大家讲道理2017-04-11 12:16:21

    随机数的格式自己再造造

    
    function test() {
        var arr = [];
        for (var i = 0; i < 100000; i++) {
            var date = new Date();
            var random = Math.random;
            var time = date.toLocaleDateString() + '-' + date.toLocaleTimeString().split(':').join('-') + '-' + Math.floor(random()*1000);
            arr.push({
                "seq": i,
                "time": time,
                "hrate": 0,
                "status": 0,
                "vol": [random(), random(), random(), random(), random()]
            })
        }
        return {
            "data": arr
        }
    }

    回复
    0
  • 阿神

    阿神2017-04-11 12:16:21

    // 初始化你的数据
    var time = 36687;
    var times = time + 1000000;
    var data = [];
    var now = "2012-07-18-21-42-13-";
    for (let m = time; m < times; m++) {
        var d = {
            hrate: 0,
            status: 0,
            vol: []
        };
        d.seq = time;
        for (let i = 0; i < 5; i++) {
            d.vol.push(Math.random() * 100);
        }
        var rand = '';
        for (let i = 0; i < 3; i++) {
            const r = Math.floor(Math.random() * 10);
            rand += r;
        }
        d.time = now + rand;
        time++;
        data.push(d);
    };
    console.log(data);
    

    回复
    0
  • 取消回复