Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

Javascript lottery numbers

I have this html:

<html>
<head>
<meta charset="UTF-8">
<title>Bonoloto</title>
<script src="bonoloto.js"></script>
<style>
    table {border-collapse: collapse;}
    td{border: 1px solid #000; text-align: center; width: 6%;}
</style>
</head>
<body>
<script>
    randomNumbers();
    tables();
</script>
</body>
</html>

And the next Javascript:

randomNumbers1 = new Array();
randomNumbers2 = new Array();
commonNumbers = new Array();

function randomNumbers() {
document.write("First line:");
for (i = 0; i< 6; i++) {
    randomNumbers1[i]=Math.floor(Math.random() * 49 + 1);
    document.write(randomNumbers1[i] + " ");
}

document.write("<br/>");

document.write("Second line:");
for (i = 0; i< 6; i++) {
    randomNumbers2[i]=Math.floor(Math.random() * 49 + 1);
    document.write(randomNumbers2[i] + " ");
}
}

function tables(){

document.write("<table>");
                var counter = 0;
            for(i = 1; i < 50; i++) {
                    counter++;
                if(counter == 11) {
                    counter = 0;
                    document.write("<tr>");
                }
                document.write("<td>" + i + "</td>");
                if(counter == 10) {
                    counter = 0;
                    document.write("</tr>");   
                }
            }
document.write("</table>");
}

How can i do to:

  • not allow same random numbers to be in the array more than 1 time.
  • the first line of numbers mark them in the table with yellow and the second line of numbers mark them with blue.
  • if the first numbers and the second numbers are the same (random numbers) mark them in the table with green.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Range of Numbers in Arrays & fisher yates shuffle

1. not allow same random numbers to be in the array more than 1 time.

to achieve that probably the only way without to many checks is to use a predefined array containing all your numbers.

function numberArray(a,b){// highest number, just a placeholder
 b=[];while(a--)b[a]=a+1;return b
}

this creates an array of numbers , where a is the highest number...

So if you want to have 90 numbers... var myNumbers=numberArray(90);

Shuffle this array. this code is based on the famous fisher yates shuffle.

function shuffleArray(d,c,b,a){//the array to shuffle, just placeholders...
 for(c=d.length-1;c>0;c--){
  b=Math.floor(Math.random()*(c+1));
  a=d[c];d[c]=d[b];d[b]=a
 }
};

to shuffle myNumbers and extract the first 6 numbers:

shuffleArray(myNumbers);
var winningNumbers=myNumbers.slice(0,6);

Demo

http://jsfiddle.net/L17968n4/

2. the first line of numbers mark them in the table with yellow and the second line of numbers mark them with blue.

if you explain that better pls, anyway using css class!

3. if the first numbers and the second numbers are the same (random numbers) mark them in the table with green.

you need to loop trough the array and check if numbers present in the first array are equal to numbers in the second array. something like that...

numbersLottery.lastIndexOF(myNumbers[0])!==-1;

this checks if the first number of myNumbers is present in the winning numbers array.

DEMO2

maybe this is what you want if i understand correctly...

http://jsfiddle.net/L17968n4/2/

DEMO3

based on comments image

http://jsfiddle.net/L17968n4/5/

and counting the correct numbers

http://jsfiddle.net/L17968n4/7/

shorter&faster shuffle function..

function shuffledArray(a,b,c,d){//array,placeholder,placeholder,placeholder
 c=a.length;
 while(c)b=Math.random()*(--c+1)|0,d=a[c],a[c]=a[b],a[b]=d;
}

PERFORMANCE vs other shuffle functions: http://jsperf.com/fyshuffle


if you have any questions about the code just ask....


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...