美文网首页
Math对象

Math对象

作者: 蛋黄肉 | 来源:发表于2017-08-12 13:42 被阅读17次

写一个函数,返回从min到max之间的 随机整数,包括min不包括max

function getRandom(min, max) {
  return Math.random() * (max - min) + min
}

写一个函数,返回从min都max之间的 随机整数,包括min包括max

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min
}

写一个函数,生成一个长度为 n 的随机字符串,字符串字符的取值范围包括0到9,a到 z,A到Z。

function getRandStr(len){
    var str=''
    arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a',
        'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 
        'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
        'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
        'U', 'V', 'W', 'X', 'Y', 'Z'];
    console.log(arr)
    for(var i=0;i<len;i++){
        str+=arr[getRandomInt(0,61)]  //使用第二题的函数
    }
    return str
}
var str = getRandStr(10); // 0a3iJiRZap

写一个函数,生成一个随机 IP 地址,一个合法的 IP 地址为0.0.0.0~255.255.255.255

function getRandIP(){
    str=getRandomInt(0,255)+','+getRandomInt(0,255)+','+getRandomInt(0,255)+','+getRandomInt(0,255)
    return str
}
var ip = getRandIP()
console.log(ip) // 10.234.121.45

写一个函数,生成一个随机颜色字符串,合法的颜色为#000000~ #ffffff

function getRandColor(){
    var str='#'
    for(var i=0;i<6;i++){
        str+=getRandomInt(0,15).toString(16)
    }
    return str
}
var color = getRandColor()
console.log(color)   // #3e2f1b

相关文章

  • JavaScript Math对象和Date对象浅谈

    JavaScript Math对象和Date对象浅谈 Math 对象 JavaScript当中的Math对象是原...

  • Date Math

    6.5 date math对象的学习 Math对象 Math对象:仅专门提供数学计算的方法Math对象没有构造函数...

  • 数值对象-Math对象

    一、Math对象的属性 Math对象的属性往往都是数学中常用到的“常量”,Math对象属性如下:Math对象属性 ...

  • Math对象

    Math对象的属性 Math对象的方法

  • 内置对象

    内置对象有 数学对象(Math对象), 日期对象(Date对象),还有String对象 Math对象: a...

  • JavaScript内置对象:math

    Math对象:math对象用于执行数学任务。 注意:Math 对象并不像 Date 和 String 那样是对象的...

  • 7_JavaScript_Math对象_字符串对象_浏览器对象B

    Math对象 Math 对象用于执行数学任务。 Math 对象并不像 Date 和 String 那样是对象的类,...

  • Math 对象

    Math 对象 Math 对象用于执行数学任务 Math 对象并不像 Date 和 String 那样是对象的类,...

  • JavaScript Math(算数) 对象

    Math(算数)对象的作用是:执行常见的算数任务。JavaScript Math 对象的参考手册 Math 对象 ...

  • Math 对象

    Math 对象用于执行数学任务 Math 对象属性 Math 对象方法 abs() abs() 方法可返回数的绝对...

网友评论

      本文标题:Math对象

      本文链接:https://www.haomeiwen.com/subject/yhodrxtx.html