应几个同事百般要求,为他们写了这个体育彩票的随机数生成程序,用到cookie.

发表于:2007-06-30来源:作者:点击数: 标签:
html head meta http-equiv=Content-Type content=text/html; charset=gb2312 title体育彩票抽奖/title script LANGUAGE= Java Script!-- /*----------------------------------------------------- 应几个同事百般要求,为他们写了这个程序,况且我从不买彩票,
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>体育彩票抽奖</title>

<script LANGUAGE="JavaScript"><!--  
/*-----------------------------------------------------
应几个同事百般要求,为他们写了这个程序,况且我从不买彩票,也不懂.
不过你要是真的中了大奖,可别忘了告诉我老蒋哟,什么?怕我分你的钱?
我不贪财的,顶多送我张classic music碟我就很欢喜了!
by jianglixin@163.net   (匆忙写就,代码零乱未及整理) 2001.2.28
-------------------------------------------------------*/
function SetCookie()
{
    var Then = new Date() ;
    if (document.all.luckNum.innerText.length>20)
    {
    Then.setTime(Then.getTime() + 1000*60*1000);
    document.cookie = "LastNum="+document.all.luckNum.innerText+";expires="+ Then.toGMTString() ;
    }
}


function GetCookie()
{    
var cookieHeader = "LastNum=";
var beginPosition = document.cookie.indexOf(cookieHeader);
var status=document.cookie.substring(beginPosition + cookieHeader.length);
document.all.lastNum.innerText="上次为您选的数: "+status;
}




function check(form)
    {
   
    if(form.inputName.value.length<1)
        {alert("请输入您的姓名!");
        form.inputName.focus();
        return(false);}
        
    if(form.inputNum.value.length<1)
        {alert("请输入您生日或幸运数字!");
        form.inputNum.focus();
        return(false);}
        
    if(!(checknum(form.inputNum.value)))
        {alert("幸运数必须是数字!");
        form.inputNum.focus();
        return(false);}

var randNum,seeds,repeat;
var my_array = new Array();
var test_array = new Array();
var sort_array = new Array();
seeds=form.inputNum.value;
selectNum=7;totalNum=36;

for (i=0;i<totalNum;i++)  sort_array[i]=i;

for (i=0;i<totalNum;i++)  test_array[i]=0;


for (p=0;p<seeds;p++)
{


var i = 0;
while (i <7)
    {    
    repeat=false;
    randNum=Math.ceil(totalNum*Math.random(seeds));

        for (j=0;j<i;j++)
            {
            if (randNum==my_array[j])
                repeat=true;
             }
             
    if (repeat==false)       
    {
    my_array[i]=randNum;
    test_array[randNum]+=1;  
    i++;    
    }
  
  }
}


//my_array排序
var temp;
for (i=0;i<selectNum-1;i++)
{
    for (j=1;j<selectNum-i;j++)
    {
        if (my_array[i]>my_array[i+j])
        {
        temp=my_array[i];
        my_array[i]=my_array[i+j];
        my_array[i+j]=temp;
        }
    }
}


//排序
var tempNum;
for (i=0;i<selectNum;i++)
{
    for (j=1;j<totalNum-i;j++)
    {
        if (test_array[sort_array][i]]<test_array[sort_array][i+j]])
        {
        tempNum=sort_array[i+j];
        sort_array[i+j]=sort_array[i];            
        sort_array[i]=tempNum;
        }
    }


}


var new_array1=new Array();
var new_array2=new Array();


for (i=0;i<selectNum;i++) new_array1[i]=sort_array[i]+1;
for (i=0;i<selectNum;i++) new_array2[i]=test_array[sort_array][i]];



str1= new_array1.join(", ");
str2= new_array2.join(", ");




str= my_array.join(", ");

document.all.luckNum.innerText="                "+str;

document.all.uname.innerText=form.inputName.value;
        
document.all.dear.style.visibility="visible";


document.all.luckNum1.innerText="另一组数("+seeds+"次统计): "+str1;
document.all.luckNum2.innerText="出现次数(与上面对应): "+str2;

        


    }
        
        
        
        
    
function checknum(num)
    {   
        var checkStr=num;
    var ch=0;    
    for(i=0;i<checkStr.length;i++){
        ch=checkStr.charCodeAt(i);
        if (ch<48 ||ch>57) return(false);
    }
    return(true);
    }
    
    
//--></script>

</head>

<body bgcolor=#eeddaa onLoad=GetCookie() onUnload=SetCookie()>


<br><br><br>

<table border=1 cellspacing=0 cellpadding=0 bgcolor=#efefef width=60% align=center>
  <form action=test2.asp method=post name=form1>
    <tr>
      <td height="58" width="31%">姓名:
        <input type=text name=inputName size=6 >
      </td>
      <td height="58" width="69%"> 幸运数:
        <input type=text name=inputNum size=4 maxlength=4 >
        <span style="font-size:12px;color=#dd0000">(可输入生日或电话号码等)</span> </td>
    </tr>
    <tr>
      <td height="51" colspan="2" align=center>
        <input type="button" name="button" value="开始" onClick="SetCookie();check(form1);GetCookie();">
          </td>
    </tr>
  </form>
</table>
<table border=1 cellspacing=0 cellpadding=0 bgcolor=#efefef width=60% align=center>
    
   
    <tr>
      
    <td  height="59" id=dear style="visibility:hidden">亲爱的 <font color=red><span id=uname></span></font>:
      您可以用这组数试试你的运气: </td>
    </tr>
    <tr>
    <td height="50"  width="33%" id=luckNum>  </td>
     
    </tr>
    <tr>
    <td height="50"  width="33%" id=lastNum> </td>
     
    </tr>
    
    <tr>
    <td height="50"  width="33%" id=luckNum1> </td>
     
    </tr>
    <tr>
    <td height="50"  width="33%" id=luckNum2> </td>
     
    </tr>
    

</table>

</body></html>
 

原文转自:http://www.ltesting.net