随机产生两个数值之间的某个值

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
	var arr = ["red","yellow","blue","green","orange","black"];
	var num = selectFn(0,arr.length-1);
	console.log(num);
	console.log(arr[num]);
});
function selectFn(min,max){
	var choices = max-min+1;
	return Math.floor(Math.random()*choices+min);
}
</script>
</body>
</html>

  

posted on 2017-05-18 13:13  jasonduanmu  阅读(319)  评论(0编辑  收藏  举报

导航