-1

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.
geoff
  • 2,251
  • 1
  • 19
  • 34
Alpha2k
  • 2,212
  • 7
  • 38
  • 65
  • `not allow same random numbers to be in the array more than 1 time.` Is not random anymore. But you can use an array `[0,1,2,3,4,5,6,7,8,9]` and just randomly pick a number out of it. – A1rPun Sep 22 '14 at 13:49
  • 1
    @A1rPun "random" does not mean "uniform distribution". – Álvaro González Sep 22 '14 at 14:35

1 Answers1

1

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....

cocco
  • 16,442
  • 7
  • 62
  • 77
  • Good answer but you should consider name your variables so that humans can read it. It also helps understanding the problem. – A1rPun Sep 22 '14 at 14:47
  • yeah, those are short codes ... where the variables are just hard to name like tempNumber1...and so on . btw i copied and past from my old code...but if you have any questions just ask, i'm happy to explain the code. – cocco Sep 22 '14 at 14:52
  • I know you put that code in your answer with good intentions but it is very confusing. It looks like your functions accept more then 1 parameter but it actually are just variables that could be written with `var`. – A1rPun Sep 22 '14 at 14:59
  • oh, that is to save bytes.. you can read more about that here http://stackoverflow.com/a/21353032/2450730 – cocco Sep 22 '14 at 15:01
  • google fisher yates shuffle and you find a longer version with nice variables or ask if you don't understand something. – cocco Sep 22 '14 at 15:03
  • I know what you are trying to do and what the functions do, I'm just trying to say if you just answer questions with cryptic code (minified or whatever) you won't receive many upvotes. – A1rPun Sep 22 '14 at 15:10
  • don't need upvotes.. just trying to help ppl to write js using usefull tricks and shorter, faster ways.like i explain in the previous link i showed you.at the end we are here to learn. – cocco Sep 22 '14 at 15:20
  • @cocco check this image http://www.mediafire.com/view/7qbhzvt67g1dysg/output.png thats what i meant by line 1 and line 2 :P ... 14,46 etc should be marked in the table with yellow and 10,1,28 blue and if any of first line number or 2nd line number are the same mark them with green in the table – Alpha2k Sep 22 '14 at 15:35
  • @cocco thanks, working as expected, even tough the code is hard to read overall I could understand what it does with your explanation. :D – Alpha2k Sep 24 '14 at 16:38