Lottery number checker

hi ,

Let me put it in a different way with words.

  1. Assume the lottery have numbers from 1-50.
  2. Out of this 50 numbers, I am going to pick up only 35 numbers randomly.
  3. so, my total numbers would be 35 numbers shuffled from [1-50] nos.
    [eg: 1,4,6,7,11,30,32,40,46,49]
  4. I have list of winning numbers in [my_number.txt] file.
  5. Now, the script should check within my 35 numbers comparing the file [my_number.txt] and should give the results .. The result number should be 6 nos.
    [eg: 1 6 30 32 46 49 ]

Thanks in Advance.

-Siva

lottery_numbers=$(seq 1 50 | shuf -n 35 | sort -n | tr '\n' ' ' ; echo ;)

for i in `seq 10`; do seq 1 50 | shuf -n 6 | sort -g | tr '\n' ' ' ;echo; done > my_numbers
# or use input file

awk '
   NR==1 {
      print "";
      print "Lottery Numbers: " lottery_numbers;
      print "";
      printf("%-25s\t%-10s\t%-10s\n", "My Numbers", "#s Matched", "Result")
      n=split(lottery_numbers, ln);
   }
   {
      c=0;
      for (i=1; i<=NF; i++) for (j=1; j<=n ; j++) if ($i==ln[j]) c++;
      printf("%-25s\t%-10d\t%-10s\n", $0, c, ((c < 6) ? "lose" : "win"));
   }
' lottery_numbers="$lottery_numbers" my_numbers

Is this not the same as https://www.unix.com/shell-programming-and-scripting/276392-lottery-result-checker.html ?

Are you actually the same person and the login names have become muddled?

Robin

Hi rdrtx1,

Thanks for your input..

Between, is there any way that I can manually input the 35 numbers rather than auto shuffle from 1-50 number to pick up only 35 nos..

-Siva

mx=50
mn=35
arr=""
c=$(echo "$arr" | wc -w)
while [ $c -lt $mn ]
do
   (( t = mn - c ))
   echo "Enter numbers from 1 to 50 ($t to go):"
   read numbers
   numbers=$(echo "$numbers" | sed 's/[;,:\//]/ /g;' | awk '
      {
         c=split(arr, a);
         for (i=1; i<=c; i++) b[a]=i;
         for (i=1; i<=NF; i++) {
            n=$i;
            n+=0;
            n=int(n);
            if (n > 0 && n<=mx && ! b[n] && split(arr, _) < mn) {arr=arr " " n; b[n]=n;}
         }
         if (arr) print arr;
      } ' mn=$mn mx=$mx arr="$arr";
   )
   arr=$(echo "$numbers" | sed 's/^ *//; s/ *$//; s/  */ /g;')
   c=$(echo "$arr" | wc -w)
   echo ""
   echo "Numbers entered: $arr"
   echo ""
done

Hi rdrtx,

I am lost here.. Do I need to use only the second script or I need to merge the first and second script together ?

-Siva

Hi ,

The second scripts only ask for the 35 numbers, so once i enter the 35 nos, its not getting me any output...

-Siva

If your shell supports arrays this change based on rdrtx1's original may suffice:

mx=50
mn=35
while (( ${#LN[@]} < mn ))
do
   read -p "Enter numbers from 1 to 50 ($((mn - ${#LN[@]})) to go): " numbers

   for num in $numbers
   do
       ((num < 1 || num > mx)) && echo "$num is invalid" || LN[num]=$num
       (( ${#LN[@]} == mn )) && break 2
   done
   echo "Numbers entered so far: ${LN[@]}"
done

for i in {1..10}; do printf "%d\n" {1..50} | shuf -n 6 | sort -g | tr '\n' ' ' ;echo; done > my_numbers
# or use input file

awk '
   NR==1 {
      print "";
      print "Lottery Numbers: " lottery_numbers;
      print "";
      printf("%-25s\t%-10s\t%-10s\n", "My Numbers", "#s Matched", "Result")
      n=split(lottery_numbers, ln);
   }
   {
      c=0;
      for (i=1; i<=NF; i++) for (j=1; j<=n ; j++) if ($i==ln[j]) c++;
      printf("%-25s\t%-10d\t%-10s\n", $0, c, ((c < 6) ? "lose" : "win"));
   }
' lottery_numbers="${LN[*]}" my_numbers

Hi Chubler,

The script worked out well with your modification.. Thanks a lot...
Between, I got what I expected.

[root@linux lottery]# sh script.sh
Enter numbers from 1 to 50 (35 to go): 2        19      47      1       46      3       6       11      15      17      26      29      32      34      40      44      48      49      5       7       9     13       22      23      25      27      45      4       8       10      14      16      18      30      35

Lottery Numbers: 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16 17 18 19 22 23 25 26 27 29 30 32 34 35 40 44 45 46 47 48 49

My Numbers                      #s Matched      Result
4 17 21 25 33 48                4               lose
10 24 26 28 32 44               4               lose
5 7 24 44 48 49                 5               lose
3 7 17 23 32 47                 6               win
5 28 34 36 38 45                3               lose
18 20 27 43 48 49               4               lose

But with this result, i need to generate the sequence of 6 nos within the 35 numbers i have mentioned and need to compare with the my_numbers file.

Is this the sort of thing you mean?

mx=50
mn=35
while (( ${#LN[@]} < mn ))
do
   read -p "Enter numbers from 1 to 50 ($((mn - ${#LN[@]})) to go): " numbers

   for num in $numbers
   do
      [[ $num =~ ^([[:digit:]])+$ ]] && ((num > 0 && num <= mx)) && LN[num]=$num ||
           echo "$num is invalid"
       (( ${#LN[@]} == mn )) && break 2
   done
   echo "Numbers entered so far: ${LN[@]}"
done

for i in {1..10}; do printf "%d\n" {1..50} | shuf -n 6 | sort -g | tr '\n' ' ' ;echo; done > my_numbers
lottery_numbers=$(printf "%d\n" ${LN[@]}| shuf -n 6 | sort -g| tr '\n' ' ')
# or use input file

awk '
   NR==1 {
      print "";
      print "Lottery Numbers: " lottery_numbers;
      print "";
      printf("%-25s\t%-10s\t%-10s\n", "My Numbers", "#s Matched", "Result")
      n=split(lottery_numbers, ln);
   }
   {
      c=0;
      for (i=1; i<=NF; i++) for (j=1; j<=n ; j++) if ($i==ln[j]) c++;
      printf("%-25s\t%-10d\t%-10s\n", $0, c, ((c < 6) ? "lose" : "win"));
   }
' lottery_numbers="$lottery_numbers" my_numbers