vue中返回随机数

<template>
  <div>
    {{  data }}
  </div>
</template>
<script>
  export default {
    name: 'index',
    data() {
      return {
        data:''
      };
    },
    created() {
      this.getRandomInt(1,10)
    },
    methods: {
      // 生成1-10的整数
      // Math.floor(); 向下舍入
      // console.log(Math.floor(3.8)); //向下舍入,小数点省略了 结果:3
      // Math.random()  random() 方法可返回介于 0(包含) ~ 1(不包含) 之间的一个随机数。
      // Math.floor((Math.random()*10)+1);取得介于 1 到 10 之间的一个随机数:
       // Math.floor((Math.random()*100)+1);取得介于 1 到 100 之间的一个随机数:
      getRandomInt(min, max) {
        // 以下函数返回 min(包含)~ max(包含)之间的数字:
       this.data = Math.floor(Math.random() * (max - min + 1)) + min

      //  函数返回 min(包含)~ max(不包含)之间的数字
      //  this.data = Math.floor(Math.random() * (max - min) ) + min;
      },
    }
  };
</script>

 

posted @ 2020-06-17 10:33  无所谓。。。  阅读(11248)  评论(0编辑  收藏  举报