前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >javaScript中的随机数方法

javaScript中的随机数方法

作者头像
山河木马
发布2019-03-05 17:25:55
1.2K0
发布2019-03-05 17:25:55
举报
文章被收录于专栏:山河木马山河木马
代码语言:javascript
复制
console.log(Math.random());
console.log(Math.random());

function getRandomFloat(min,max) {
    return Math.random()*(max - min) + min;
}
console.log(getRandomFloat(5,10));

function getRandomInt(min,max) {
    return Math.floor(Math.random()*(max - min) + min);
}
console.log(getRandomInt(5,10));

function getRandomInRange(min,max) {
    return Math.floor(Math.random()*(max - min + 1)) + min;
}
console.log(getRandomInRange(1,3));

function coinToss() {
    return Math.floor(Math.random() * 2);
}
console.log(coinToss());

function coinToss_t() {
    return Math.floor(Math.random() * 2) === 0;
}
console.log(coinToss_t());

var numPool = [5,9,75,54,32,14,25,6];
rand = numPool[Math.floor(Math.random() * numPool.length)];
console.log(rand);


var numPools = [65,32,41,2,7,6,85,32,12,17,26,24];
function shuffle(numPools) {
    for (var j,x,i = numPools.length;i;j = parseInt(Math.random() * i),x = numPools[--i],numPools[i] = numPools[j],numPools[j] = x) {
        return numPools;
    }
}
var randomResult = shuffle(numPools);
while (randomResult.length > 0) {
    console.log(randomResult.pop());
}

var cryptoStor = new Uint16Array(8);
var Cryptography = window.crypto.getRandomValues(cryptoStor);
console.log(Cryptography);

学习地址:http://www.htmleaf.com/ziliaoku/qianduanjiaocheng/201612054242.html

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017/05/09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档