Question

I've got a simple bash script that generates the following:

These are your winning lottery numbers: 
 27  6   29  17  15  47
 19  16  33  15  20  14
 29  34  48  19  33  40

Here is the code for it:

#!/bin/bash

tickets="$1"

function get_tickets { printf "How many tickets are you going to get? "; read tickets;}

function gen_numbers { printf "\nThese are your winning lottery numbers: \n"; 

    for ((z=1 ; z<=tickets ; z++)); do
         for ((i=0; i<6; i++ )); do 
            x=`echo $[ 1 + $[ RANDOM % 49 ]]`; 
            printf "\t $x"; 
         done;
         printf "\n" 
    done; 
    printf "\n"; }


############################
if [[ -z $tickets ]]  ; then
    get_tickets
    gen_numbers
else
    gen_numbers
fi

My question is, does anyone know how to modify it to prevent duplicate numbers on each row from appearing? I am guess I'd use uniq, and an array, but am not sure how that would look. Any advice would be appreciated; thanks!

  • This is just a script for fun.
Was it helpful?

Solution 2

To avoid duplicates by row use an array to mark the used values. Here is a modified version of your script:

for ((z = 1; z<=tickets ; z++)); do
     i=0
     used=( 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 )
     for (( ; i<6;  )); do 
        x=`echo $[ 1 + $[ RANDOM % 49 ]]`; 
        if [ ${used[$x]} -eq 0 ]
        then
            printf "\t $x"; 
            i=$((i + 1))
            used[$x]=1
        fi
     done;
     printf "\n" 
done; 
printf "\n"; }

EDIT: Explanation.

The idea is very simple, we use the array to mark those values already used.

Initially all the elements (50 elements that corresponds to the values [0, 49]) of the array are set to zero, i.e. all the numbers are available.

For each random number x we check if that number is available (used[x] = 0), if so we increase the counter i and mark that value (used[x] = 1) to avoid repetitions.

If the value is already taken simply try again and again until an unused value is found.

OTHER TIPS

Your attempt is pretty good. However, I think it can be easier and safer to get random values by using the shuf command:

$ shuf -i 1-49 -n18 | xargs -n6
39 42 43 7 14 23
10 27 5 13 49 8
31 36 19 47 28 4

shuf -i X-Y -nZ gives Z random numbers in between X and Y. Then xargs -nT formats them in groups of T numbers per line.


Update

Now I see the comment:

Yes; to avoid duplicate numbers within a row (by ticket).

In that case, you can simply do shuf -i 1-49 -n6 to get 6 random numbers. The output is line separated, so you can use tr '\n' ' ' to make it space separated.

In case you want many rows, for example 5, you can do:

for i in {1..5}; do shuf -i 1-49 -n6; done | xargs -n6

Sample output:

$ for i in {1..5}; do shuf -i 1-49 -n6; done | xargs -n6
4 45 12 42 37 46
42 20 29 22 12 5
40 41 14 28 4 2
35 24 16 22 2 39
14 46 47 20 21 41

Check this one, it's quite good example of implementing lotto numbers generator using just JavaScript: https://lotto6aus49online.de/#zaufallszahlen; you can stop the generator by your own click for every number, which adds additional randomness into the algorithm. hope you like it!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top