level 6
//思路给你了,自己修改吧
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<script>
function chouqu(){
var myarr = ["A","B","C","D","E","F"];
var i = Math.floor(Math.random()*6);
var y = 5-i;
document.getElementById("demo1").innerHTML=myarr[i];
document.getElementById("demo2").innerHTML=myarr[y];
}
</script>
</head>
<body>
<input type="button" value="抽取" onclick="chouqu()" />
<p id="demo1"></p>
<p id="demo2"></p>
</body>
</html>
2015年11月09日 14点11分
4
Math.random()*6 能问下这里乘6是什么意思?
2015年11月12日 02点11分
@JACKRTED random()是取0-1之间的数。乘以6是取0-5之间的随机数。
2015年11月12日 03点11分
@晶莹状雪花 因为random是取不到1的,只能无限接近1,所以乘以6只能无限接近6。再加上Math.floor函数,那么值就取到了0到5之间的整数了。
2015年11月12日 03点11分
@晶莹状雪花 明白了,也就是要求26个字母的话乘以个26吧,受教。
2015年11月12日 03点11分
level 1
<p id="myram"></p>
<script>
function fnRandom(){
var az = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var iMath = function(){
return Math.floor(Math.random()*26);
}
var iMa = function(i,n){
while (n === i){
n = iMath();
if (n !== i) return n;
}
}
var x = iMath(),
y = iMath(),
y = (x === y) ? iMa(x,y) : y,
result = az.charAt(x) + '' +az.charAt(y);
document.getElementById('myram').innerHTML = result;
}
setInterval(fnRandom,1000);
</script>
2015年11月10日 06点11分
6
level 9
说下方法,1~26对应26个英文字母,随机取出26个数字,且不重复
2015年11月10日 11点11分
11
level 6
<script>
function getTwoRandom(){
var array = ['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'];
var x,y;
x=Math.floor(Math.random()*(25-0+1)+0);
// console.log(x);
function getY(){
y=Math.floor(Math.random()*(25-0+1)+0);
// console.log(y);
if(y==x){
getY();
}
}
getY();
return array[x]+array[y];
}
//console.log(getTwoRandom());
function randomStore(size){
var randomSet=new Set();
for(var i=0;i<size;i++){
var item=getTwoRandom();
randomSet.add(item);
}
return randomSet;
// for(var setItem of randomSet){
// console.log(setItem);
// }
}
//100个不同的两不同字母组合可选
console.log(Array.from(randomStore(100))[1]);
</script>
不知道是不是可以这样 刚学不久...希望有大牛看到指点一下 其中的缺陷
2015年11月10日 13点11分
14
错得挺多的。。。
2015年11月10日 17点11分
回复 yoyo_qiqi1314 :又仔细看了一下,你这样写是结果应该是对的,我之前看差了。不过用递归实现getY确实不好,没必要增加栈的深度,空间开销大。
2015年11月11日 17点11分
回复 yoyo_qiqi1314 :第一个array可以用字符串,也可以用fromCharCode函数实现。
2015年11月11日 17点11分
level 13
function getCharPair(){
n=Math.floor(Math.random()*650);
return String.fromCharCode(0x41+n%26)+String.fromCharCode(0x41+(n+n/26)%26);
}
2015年11月10日 22点11分
15
后面一个数再模26前再加1就对了,因为n/26取值在0-24之间
2015年11月11日 01点11分
@沙洲客🍼 呵呵,你说的对,最后+1,加0重复了
2015年11月11日 21点11分