js产生两组随机数,第一组产生不重复随机数组,第二组产生不包括第一组数据的不重复随机数

lxn460896944 2013-01-08 07:54:33
这是我在做下面的抽奖小系统才有这样的问题:
在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>
...全文
416 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
鸡肋啊 2013-01-09
  • 打赏
  • 举报
回复
这个不需要用cookie也能实现的吧!把test1.jsp产生出来的随机数提交到后台或者放到session中,然后再在test2.jsp中取出来做对比
scscms太阳光 2013-01-09
  • 打赏
  • 举报
回复
引用 7 楼 lxn460896944 的回复:
这是一个页面,在两个页面之间怎么做呢
代码一样呀,可复制在相同站点下多个文件使用,关键是cookie相通即可。
lxn460896944 2013-01-09
  • 打赏
  • 举报
回复
引用 6 楼 xzy21com 的回复:
大概是这个意思,但我的cookie写得过于简单,你找个专业点的函数换了即可。
这是一个页面,在两个页面之间怎么做呢
scscms太阳光 2013-01-09
  • 打赏
  • 举报
回复
大概是这个意思,但我的cookie写得过于简单,你找个专业点的函数换了即可。
scscms太阳光 2013-01-09
  • 打赏
  • 举报
回复
<script type="text/JavaScript">
    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>
lxn460896944 2013-01-09
  • 打赏
  • 举报
回复
引用 2 楼 aaronteng922 的回复:
代码如下,请根据自己的需求另调整。 JavaScript code?12345678910/* 页面1代码 */ var random1 = Math.round(Math.random() * (500 - 1) + 1),// 随机数1 random2 = Math.round(Math.random() * (500 - 1……
恩 麻烦你写这么多代码,我现在做一个系统,关于抽奖的,不如第一组抽50个不重复的随机数,在第二组中抽取不包括第一组的随机数,实现起来和你写的一样吗,或者说该怎么实现
lxn460896944 2013-01-09
  • 打赏
  • 举报
回复
引用 1 楼 xzy21com 的回复:
抽中的号存cookies呀,点停止时判断一下抽出来的号在不在cookies里,在就重抽不就行了。
恩 谢谢你 我试下
我本JS菜鸟 2013-01-08
  • 打赏
  • 举报
回复
代码如下,请根据自己的需求另调整。
/*
		页面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);
	}
scscms太阳光 2013-01-08
  • 打赏
  • 举报
回复
抽中的号存cookies呀,点停止时判断一下抽出来的号在不在cookies里,在就重抽不就行了。

87,991

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧