JavaScript based on the implementation of a simple random lottery applet

  • 2020-11-20 05:58:19
  • OfStack

For small programs such as lottery, VB, Delphi and other tools to achieve will be more convenient, because I do not have such an application on the machine, so can only find another way. In order for the lottery program to be able to run directly on any one machine without a configuration platform, the development and compilation tools could also be simple (such as text text can be edited and window's browser can be compiled and run), so I decided to try using javascript. My research on javascript is not deep. It is mainly used to judge the validity of data from the client in website development (for security reasons, websites with high security requirements should try to use server-side language to judge the validity of data), which involves the most commonly used methods and functions. Therefore, the following program can only be called relatively humble, interface effects and functions are very "plain".

The key point is to select random Numbers. The formula (upper limit of the range - lower limit of the range +1)* Math. random()+ lower limit of the range is used to select random Numbers within the set range. Here is the source code:


<html> 
<head> 
<title>  Lucky Draw applet  </title> 
<!--javascript Program - Begin--> 
<script language="javascript"> 
// Lucky draw number range initialized  
var from=1; // The starting value  
var to=1523; // Termination of the value  
numarray=new Array(); // Holds an array of lucky draw Numbers  
flagarray=new Array(); // An array of tokens that record whether a number has won a prize  
countaward=new Array(0,0,0); // Record the number of draws per group. Only draw here 3 Group award  
/** 
 The function name :sysInit 
 Incoming parameters : There is no  
 The return value :ture/false 
 function : Lottery system initialization , Sets the lucky draw number range, initializes the lucky draw number array and marks array  
*/ 
function sysInit() 
{ 
/* Set the lucky draw number range */ 
// Accept user - set actions  
mixNum=prompt(' The starting value ',1); 
maxNum=prompt(' Termination of the value ',1523); 
// Determines that the user did not enter any data or null characters  
if(mixNum!=null&&maxNum!=null&&mixNum!=""&&maxNum!="") 
{ 
// Determine if the data entered by the user is a valid number  
strTemp="0123456789"; 
for (i=0;i<(mixNum+maxNum).length;i++) 
{ 
j=strTemp.indexOf((mixNum+maxNum).charAt(i)); 
if (j==-1) 
{ 
alert(" Incorrect starting number range , Program interrupt !"); 
return false; 
}//end if 
}//end for 
// If the user enters a valid number, the lottery scope is reset  
from=parseInt(mixNum); 
to=parseInt(maxNum); 
}//end if 
// Initializes the lucky draw number array and identifier array  
for(i=0;i<(to-from);i++) 
{ 
numarray[i]=from+i; 
flagarray[i]=0; 
} 
// Lucky draw button is valid  
first.disabled=false; 
second.disabled=false; 
third.disabled=false; 
return true; 
} 
/** 
 The function name :getLuck 
 Incoming parameters : JiangCi award, Total number of prizes awardtotal 
 The return value : There is no  
 function : No repeat draw  
*/ 
function getLuck(award,awardtotal) 
{ 
var msg=""; 
// When the number of draws is greater than or equal to 20 When a , Use each extraction 10 The number of winners.  
for(i=0;i<(awardtotal>=20?10:1);i++) 
{ 
// Set the loop to extract random Numbers and determine , Prevent duplicate fetching of Numbers  
while(a=1) 
{ 
// The decision indicates that a certain prize has been won  
if(countaward[award-1]==awardtotal) 
{ 
alert(award+" The prize is full "+awardtotal+" a "); 
return; 
} 
// Draw a random number within the lucky draw number range  
lucky=Math.round((to-from+1)*Math.random()+from); 
// Determine if the random number extracted above has been taken  
if(numarray[lucky-from]==lucky&&flagarray[lucky-from]==0) 
{ 
flagarray[lucky-from]=award; 
countaward[award-1]++; 
msg+=award+" Award, N"+countaward[award-1]+":"+lucky+"\n"; 
break; 
}//end if 
}//end while 
}//end for 
alert(msg); 
return; 
} 
/** 
 The function name :showLuck 
 Incoming parameters : There is no  
 The return value : There is no  
 function : Displays all the winning Numbers  
*/ 
function showLuck() 
{ 
var str1="1 Award: <p>"; 
var str2="2 Award: <p>"; 
var str3="3 Award: <p>"; 
for(i=0;i<(to-from);i++) 
{ 
switch(flagarray[i]) 
{ 
case 1: 
str1=str1+numarray[i]+"<br>"; 
break; 
case 2: 
str2=str2+numarray[i]+"<br>"; 
break; 
case 3: 
str3=str3+numarray[i]+"<br>"; 
break; 
} 
} 
// According to 3 The number of winners in the awards  
document.write(str1); 
document.write(str2); 
document.write(str3); 
} 
</script> 
<!--javascript Program - End--> 
</head> 
<body> 
<center> 
<p><strong><font size="3" color="red"> Let's start the lottery. !!!</font></strong></p> 
<input type="button" name="range" value=" Setup lottery system " onclick="javascript:sysInit();"><p> 
<input type="button" name="first" value=" extracting 1 Award, " disabled onclick="javascript:getLuck(1,3);"><p> 
<input type="button" name="second" value=" extracting 2 Award, " disabled onclick="javascript:getLuck(2,20);"><p> 
<input type="button" name="third" value=" extracting 3 Award, " disabled onclick="javascript:getLuck(3,100);"><p> 
<input type="button" name="show" value=" Display lucky draw results " onclick="javascript:showLuck();"><p> 
</center> 
</body> 
</html> 

JavaScript based on the implementation of a simple random lottery small program code to this end, the above code comments to write more detailed, do not understand the place welcome to propose, this site this site will be in the first time to give you a reply, thank you for the support of the site site.


Related articles: