随机数的实际应用和方法

116 阅读3分钟

随机数 - random()

向上取整 - Math.ceil()

arduino
console.log( Math.ceil(3.14) );

向下取整 - Math.floor()

arduino
console.log( Math.floor(4.99999) );

四舍五入 - Math.round()

javascript
console.log( Math.round(3.14) );

求最大值 - Math.max(多个数字)

javascript
console.log( Math.max(1,5,9,7,6,4,3,8,2) );

最小值 - Math.min(多个数字)

javascript
console.log( Math.min(1,5,9,7,6,4,3,8,2) );

求次方 - Math.pow(底数, 幂)

arduino
console.log( Math.pow(2, 3) );

开平方根 - Math.sqrt(数字)

arduino
console.log( Math.sqrt(4) );

圆周率 - Math.PI

javascript
console.log( Math.PI );

正弦 - Math.sin(弧度)

javascript
1弧度 = 半径
360度的弧度 = 2π * 半径
30度的弧度 = 2π * 半径 / 360 * 角度
console.log( Math.sin(30  * 2 * Math.PI / 360) );

余弦 - Math.cos(弧度)

javascript
console.log( Math.cos(60 * Math.PI * 2 / 360) );

进制转换

将10进制转成其他进制

vbscript
 数字.toString(目标进制数)

var a = 3
console.log( a.toString(2) );

将其他进制转成10进制

ini
parseInt(要转的数字, 将他看做多少进制)

var a = '11'
console.log( parseInt(a, 2) );
console.log( parseInt(a, 8) );

获取随机的颜色值,并通过点击浏览器窗口改变body的背景颜色
var color = '#'
for (var a = 0; a < 3; a++) {
    var num = getRandom(0, 256)
    num = num.toString(16)
    // 通过判断,一定要保证每次得到的随机16进制 是两位数
    if (num.length < 2) {
        num = '0' + num
    }
    color += num
}
console.log(color);
// 代表body标签 --- >    window.document.body
// 设置标签的样式 --->    标签.style.css键 = 值
// window.document.body.style.backgroundColor = color
window.onclick = function() {
    window.document.body.style.backgroundColor = getColor()
}

定时器

javascript
  setInterval(function() {
        // 每隔一段时间要执行的代码
    }, 毫秒数)

    返回一个数字,代表当前页面中的第几个定时器 - 利用返回值停止定时器
    clearInterval(返回值)

var timer = setInterval(function() {
    window.document.body.style.backgroundColor = getColor()
}, 1000)

btn.onclick = function() {
    clearInterval(timer)
    console.log(timer);
}

javascript setInterval(function() { console.log(1); }, 1)

for (var a = 1; a <= 10000; a++) { console.log(2); }


### 双色球案例

scss // 双色球:6个红球 + 1个蓝球 // 红球:131 - 每个数字都不可以重复 // 蓝求:116

// 定义数组 var arr = [] // 循环创建6个随机红色号码 for (var a = 0; a < 6; a++) { // 生成随机数 getRandom需要调用自己封装生成的随机数 var num = getRandom(1, 32) // 判断num是否在数组中 var index = arr.indexOf(num) if (index >= 0) { a-- continue } // 将随机数放在数组中 arr.push(num) } // console.log(arr); // 生成蓝色号码 var lan = getRandom(1, 17) arr.push(lan) console.log(arr); arr.forEach(function(item) { document.write('

  • '+item+'
  • '); })

    
    ## 时间日期处理的对象
    
    

    javascript var date = new Date() 默认在Date的小括号中没有实参 - 就表示当前时间(指的是当前计算机的时间) 非当前时间,需要在小括号中放实参 实参: '年-月-日 时:分:秒'

    多个数字   
        年,月,日,时,分,秒
    
    时间戳:使用毫秒数来描述一个时间的
        从1970年1月1日0点0分0秒开始计算毫秒数
    

    // 注意月份:用011来描述 112月 var date = new Date(2023,9,1,8,0,0) console.log(date);

    
    ## 获取具体的时间日期
    
    ### 获取年份
    
    

    ini var year = date.getFullYear() console.log(year);

    
    ### 获取月份
    
    

    ini var month = date.getMonth() + 1 console.log(month);

    
    ### 获取日
    
    

    ini var d = date.getDate() console.log(d);

    
    ### 获取星期几
    
    

    ini var day = date.getDay() console.log(day);

    
    ### 获取时
    
    

    ini var hour = date.getHours() console.log(hour);

    
    ### 获取分
    
    

    ini var minute = date.getMinutes() console.log(minute);

    
    ### 获取秒
    
    

    ini var second = date.getSeconds() console.log(second);

    
    ### 获取毫秒
    
    

    ini var milisecond = date.getMilliseconds() console.log(milisecond);

    
    ### 获取时间戳
    
    

    lua var time = date.getTime() console.log(time);

    
    ### 计算100天以前是几月几日案例
    
    

    javascript // 获取当前时间 - 100天 var date = new Date() // 转成时间戳 var time = date.getTime() // 减去100天的毫秒数 var diff = time - 100 * 24 * 60 * 60 * 1000 // 100天以前的那个时间的时间戳 // 根据结果获取具体的月份和日期 - 依赖时间日期对象 var newDate = new Date(diff) console.log(newDate); // 获取月份和日期 var month = newDate.getMonth() + 1; var d = newDate.getDate() console.log('100天以前是'+month+'月'+d+'日');

    
    ### 距离国庆还有几天几小时几分钟
    
    

    javascript // 定义国庆的时间日期对象 var guoqing = new Date('2023-10-1 0:0:0') // 定义当前的时间日期对象 var now = new Date() // 求两个时间日期对象对应的时间戳的差 var diff = guoqing.getTime() - now.getTime() // 计算对应的天、小时、分钟 var day = parseInt(diff / 1000 / 60 / 60 / 24) console.log(day); var hour = parseInt(diff / 1000 / 60 / 60) % 24 console.log(hour); var minute = parseInt(diff / 1000 / 60) % 60 console.log(minute);

    
    ## 设置具体的时间日期
    
    ### 设置月份
    
    

    lua date.setMonth(9) console.log(date);

    
    ### 设置年份
    
    

    lua date.setFullYear(2025) console.log(date);

    
    ### 设置日期
    
    

    lua date.setDate(29) console.log(date);

    
    ### 设置时
    
    

    lua date.setHours(23) console.log(date);

    
    ### 设置分
    
    

    lua date.setMinutes(59) console.log(date);

    
    ### 设置秒
    
    

    lua date.setSeconds(59) console.log(date);

    
    ## 设置时间戳
    
    

    lua date.setTime(0) console.log(date);

    
    ### 封装格式式化时间日期的函数
    
    

    ini // 2023年9月14日 星期四 14点38分22秒 var date = new Date() var guoqing = new Date('2023-10-1 5:21:30') console.log(dateFormat(date)); console.log(dateFormat(guoqing)); // 函数处理重复

    function dateFormat(date) {
        var year = date.getFullYear()
        var month = date.getMonth() + 1
        var d = date.getDate()
        var arr = ['日', '一', '二', '三', '四', '五', '六']
        var day = date.getDay()
        var hour = date.getHours()
        var minute = date.getMinutes()
        var second = date.getSeconds()
        var str = year + '年' + month + '月' + d + '日 星期' + arr[day] + ' ' + hour + '点' + minute + '分' + second + '秒'
        return str
    }