这是我在做下面的抽奖小系统才有这样的问题:
在test1.jsp在1-500中产生不重复随机两个数12,23,在第二个页面test2.jsp产生的两个数中不包括12,23,该怎么实现(所有产生的随机数不重复)
//test1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'tpl.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" src="jQuery.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript">
var c=0
var t
function start()
{
var txt1=Math.round(Math.random() * (500- 1) + 1);
var txt2=Math.round(Math.random() * (500- 1) + 1);
document. getElementById('txt1').value=txt1;
document. getElementById('txt2').value=txt2;
t=setTimeout("start()",60)
}
function over()
{
clearTimeout(t);
}
</script>
</head>
<body>
<input type="text" id="txt1" style="width:60px;height:50px;font-size:30px;">
<input type="text" id="txt2" style="width:60px;height:50px;font-size:30px;">
<div class="start" ><img src="images/start1.png" onclick="start()"/></div>
<div class="over"><img src="images/over.png" onclick="over()" /></div>
</body>
</html>
//test2.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'tpl.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" src="jQuery.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript">
function start()
{
var txt3=Math.round(Math.random() * (500- 1) + 1);
var txt4=Math.round(Math.random() * (500- 1) + 1);
document. getElementById('txt3').value=txt3;
document. getElementById('txt4').value=txt4;
t=setTimeout("start()",60)
}
function over()
{
clearTimeout(t);
}
</script>
</head>
<body>
<li><input type="text" id="txt3" style="width:60px;height:50px;font-size:30px;"></li>
<li><input type="text" id="txt4" style="width:60px;height:50px;font-size:30px;"></li>
<div class="start" ><img src="images/start1.png" onclick="start()"/></div>
<div class="over"><img src="images/over.png" onclick="over()" /></div>
</body>
</html>
在test1.jsp在1-500中产生不重复随机两个数12,23,在第二个页面test2.jsp产生的两个数中不包括12,23,该怎么实现(所有产生的随机数不重复)
//test1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'tpl.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" src="jQuery.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript">
var c=0
var t
function start()
{
var txt1=Math.round(Math.random() * (500- 1) + 1);
var txt2=Math.round(Math.random() * (500- 1) + 1);
document. getElementById('txt1').value=txt1;
document. getElementById('txt2').value=txt2;
t=setTimeout("start()",60)
}
function over()
{
clearTimeout(t);
}
</script>
</head>
<body>
<input type="text" id="txt1" style="width:60px;height:50px;font-size:30px;">
<input type="text" id="txt2" style="width:60px;height:50px;font-size:30px;">
<div class="start" ><img src="images/start1.png" onclick="start()"/></div>
<div class="over"><img src="images/over.png" onclick="over()" /></div>
</body>
</html>
//test2.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'tpl.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" src="jQuery.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript">
function start()
{
var txt3=Math.round(Math.random() * (500- 1) + 1);
var txt4=Math.round(Math.random() * (500- 1) + 1);
document. getElementById('txt3').value=txt3;
document. getElementById('txt4').value=txt4;
t=setTimeout("start()",60)
}
function over()
{
clearTimeout(t);
}
</script>
</head>
<body>
<li><input type="text" id="txt3" style="width:60px;height:50px;font-size:30px;"></li>
<li><input type="text" id="txt4" style="width:60px;height:50px;font-size:30px;"></li>
<div class="start" ><img src="images/start1.png" onclick="start()"/></div>
<div class="over"><img src="images/over.png" onclick="over()" /></div>
</body>
</html>
/*
页面1代码
*/
var random1 = Math.round(Math.random() * (500 - 1) + 1),// 随机数1
random2 = Math.round(Math.random() * (500 - 1) + 1);// 随机数2
document.cookie = encodeURIComponent("random1") + "=" + encodeURIComponent(random1);
document.cookie = encodeURIComponent("random2") + "=" + encodeURIComponent(random2);
document.writeln("随机数1:" + random1);
document.writeln("随机数2:" + random2);
/*
页面2代码。
只实现页面2不能和页面1的随机数一样。若需要页面1和页面2一样的功能,单独抽出公共的代码放到.js文件中,稍修改即可。
*/
var cookie = document.cookie ,
encodeRandom1 = encodeURIComponent("random1"),
encodeRandom2 = encodeURIComponent("random2"),
random1Start = cookie.indexOf(encodeRandom1 + "="),
random2Start = cookie.indexOf(encodeRandom2 + "="),
random2End,random1End,
cookieRandom1 , cookieRandom2,
random3 = Random(),
random4 = Random(),
values = [],
i,len;
if(random1Start > -1){
random1End = cookie.indexOf(";" , random1Start);
} if(random2Start > -1){
random2End = cookie.indexOf(";" , random2Start);
} cookieRandom1 = decodeURIComponent(cookie.substring(random1Start + encodeRandom1.length + 1 , random1End ));
cookieRandom2 = decodeURIComponent(cookie.substring(random2Start + encodeRandom2.length + 1 , random2End ));
values.push(cookieRandom1);
values.push(cookieRandom2); random3 = getCompareRandom(random3);
random4 = getCompareRandom(random4); document.cookie = encodeURIComponent("random1") + "=" + encodeURIComponent(random3);
document.cookie = encodeURIComponent("random2") + "=" + encodeURIComponent(random4);
document.writeln("随机数3:" + random3);
document.writeln("随机数4:" + random4); function getCompareRandom (random){
for(len = values.length - 1 , i = len ; i >= 0 ; i--){
if(compare(values[i] , random)){
random = Random();
arguments.callee(random);
}
}
return random;
} function compare(cookie,random){
if(cookie == random){
return true;
}
} function Random(){
return Math.round(Math.random() * (500 - 1) + 1);
}
function getRound(str){
var tmp,reg=new RegExp("(^| )scscms=([^;]*)(;|$)","gi");
if(tmp=reg.exec(document.cookie)){
if(tmp[2].indexOf(str)==-1){
document.cookie="scscms="+tmp[2]+"_"+str;
document.getElementById("scscms").innerHTML=tmp[2]+"_"+str;
return str;
}else{
return getRound(str);
}
}else{
document.cookie="scscms="+str;
return str;
}
}
var txt3,txt4,t;
function fun_start(){
txt3=Math.round(Math.random() * (500- 1) + 1);
txt4=Math.round(Math.random() * (500- 1) + 1);
document.getElementById('txt3').value=txt3;
document.getElementById('txt4').value=txt4;
t=setTimeout("fun_start()",60);
}
function fun_over(){
clearTimeout(t);
document.getElementById('txt3').value=getRound(txt3);
document.getElementById('txt4').value=getRound(txt4);
}
</script>
<ul>
<li>曾经抽中的号:<span id="scscms"></span></li>
<li><input type="text" id="txt3" style="width:60px;height:50px;font-size:30px;"></li>
<li><input type="text" id="txt4" style="width:60px;height:50px;font-size:30px;"></li>
<li><input type="button" value="开始" onclick="fun_start()"></li>
<li><input type="button" value="结束" onclick="fun_over()"></li>
</ul>