Using Bash to Generate Lotto Numbers - arrays

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.

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

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.

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!

Related

Identify position of a word in a line stored in a variable using Powershell

I have a text file which is stored in a variable say $RC. It looks like below.
Total Copied Skipped Mismatch FAILED Extras
Dirs : 49 10 0 0 0 0
Files : 212 170 37 0 5 2
Bytes : 6.517 t 6.517 t 24.5 k 0 136.37 m 550
When I run
$RC | Measure-Object -Word -character -Line
it gives me the output as
Lines Words Characters Property
----- ----- ---------- --------
4 34 280
if I run $RC[1], it gives me the first line as:-
Dirs : 39 9 0 0 0 0
Now I want to navigate to 7th word in this above line (which is 0), how do I do that?
If that's not possible, my ultimate goal is to find values under Failed column (which are 0,5,136.37) in above text file and store them in another variable for comparison. How can it be done? Thank you in advance.
In case you don't want tot reinvent the wheel; using this ConvertFrom-SourceTable cmdlet:
$RC = '
Total Copied Skipped Mismatch FAILED Extras
Dirs : 49 10 0 0 0 0
Files : 212 170 37 0 5 2
Bytes : 6.517 t 6.517 t 24.5 k 0 136.37 m 550'
$Data = ConvertFrom-SourceTable -Literal $RC
$Data |Format-Table
Total Copied Skipped Mismatch FAILED Extras
----- ------ ------- -------- ------ ------
Dirs : 49 10 0 0 0 0
Files : 212 170 37 0 5 2
Bytes : 6.517 t 6.517 t 24.5 k 0 136.37 m 550
Taking the first -zero based ([0])- row and column Extras:
$Data[0].Extras
0
and taking the whole failed column using Member-Access Enumeration:
$Data.Failed
0
5
136.37 m

How does 'While' loop and 'for' loop build in programming languages?

suddenly a question goes over my mind which is how the WHILE loop and FOR loop has been built into programming languages
and can we make something like it and write it the same way we write the loops?
I tried to make this code in dart language but it doesn't work and doesn't seem to be like the normal FOR loop
dynamic forLoop(condition, body){
if(condition){
body();
forLoop(condition, body);
}
}
void main(){
forLoop( (Something.i<5), (){print(Something.i); Something.i++;} );
}
I saw a similar question for Python for loop.
I tried the same approach using dis module for while loop.
def f():
n = 0
while n < 2:
print("body")
n += 1
return None
dis.dis(f)
2 0 LOAD_CONST 1 (0)
2 STORE_FAST 0 (n)
3 4 LOAD_FAST 0 (n)
6 LOAD_CONST 2 (2)
8 COMPARE_OP 0 (<)
10 POP_JUMP_IF_FALSE 18 (to 36)
4 >> 12 LOAD_GLOBAL 0 (print)
14 LOAD_CONST 3 ('body')
16 CALL_FUNCTION 1
18 POP_TOP
5 20 LOAD_FAST 0 (n)
22 LOAD_CONST 4 (1)
24 INPLACE_ADD
26 STORE_FAST 0 (n)
3 28 LOAD_FAST 0 (n)
30 LOAD_CONST 2 (2)
32 COMPARE_OP 0 (<)
34 POP_JUMP_IF_TRUE 6 (to 12)
6 >> 36 LOAD_CONST 0 (None)
38 RETURN_VALUE
You can find details about Python Bytecode Instructions in the dis module.

AWK array seems to empty itself?

I am trying to learn awk by solving code puzzles. I am trying to read several "grids" of integers (representing bingo boards as per https://adventofcode.com/2021/day/4) into a three-dimensional awk array. An example "grid" can look like this:
22 13 17 11 0
8 2 23 4 24
21 9 14 16 7
6 10 3 18 5
1 12 20 15 19
And there are several of these in a longer input file. After reading each line into an array rows I am attempting to organize the numbers into this multi dimensional array called boards. Here is my example code:
{
b = 0
for (i in rows) {
split(rows[i], nums, " ")
for (j in nums) {
r = i % 5
n = j - 1
boards[b][r][n] = nums[j]
print b, r, n, nums[j], boards[b][r][n]
}
if (i%5==0)
++b
}
print boards[0][1][1]
}
Notice the debug printout print b, r, n, nums[j], boards[b][r][n] which indeed outputs the correct values for boards[b][r][n] on that row:
0 0 0 22 22
0 0 1 13 13
0 0 2 17 17
Etc. This seems to verify that the multi dimensional array gets written properly. Yet on the final line of the example code, the output is instead empty. I have tried using the form boards[b, r, n] for the array as well with the exact same result. Obviously there's something I'm not quite understanding here. Any help is appreciated. Full code for reproducibility:
# === ex.txt ===
7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
22 13 17 11 0
8 2 23 4 24
21 9 14 16 7
6 10 3 18 5
1 12 20 15 19
3 15 0 2 22
9 18 13 17 5
19 8 7 25 23
20 11 10 24 4
14 21 16 12 6
14 21 17 24 4
10 16 15 9 19
18 8 23 26 20
22 11 13 6 5
2 0 12 3 7
# === solve.awk ===
BEGIN {
r = 0
}
{
if (NR == 1)
split($0, draws, "")
else if (NR != 2 && (NR-3)%6 != 5)
rows[r++] = $0
}
END {
b = 0
for (i in rows) {
split(rows[i], nums, " ")
for (j in nums) {
r = i % 5
n = j - 1
boards[b][r][n] = nums[j]
print b, r, n, nums[j], boards[b][r][n]
}
if (i%5==0)
++b
}
print boards[0][1][1]
}
I run this with awk -f solve.awk ex.txt. awk --version outputs GNU Awk 5.1.1, API: 3.1 (GNU MPFR 4.1.0-p13, GNU MP 6.2.1) as its first line. Thank you!
You are incrementing b at the end of the first iteration of your for (i in rows) loop because i == 0 ==> i%5 == 0, while you want to do it at the end of the 5th iteration. Try if (i%5 == 4) ++b.
Note that as you use GNU awk you could simplify all this. When the record separator (RS) is the empty string the records are separated by empty lines (one record per board):
$ awk -v RS='' '
NR>1 {
a[NR-2][1]; split($0, a[NR-2]);
}
END {
for(b in a) for(r in a[b])
boards[b][int((r-1)/5)][(r-1)%5] = a[b][r];
for(b in boards) for(r in boards[b]) for(n in boards[b][r])
print b, r, n, boards[b][r][n]
}' ex.txt
0 0 0 22
0 0 1 13
0 0 2 17
0 0 3 11
0 0 4 0
0 1 0 8
...

Same column of different files into the same new file

I have multiple folders Case-1, Case-2....Case-N and they all have a file named PPD. I want to extract all 2nd columns and put them into one file named 123.dat.
It seems that I cannot use awk in a for loop.
case=$1
for (( i = 1; i <= $case ; i ++ ))
do
file=Case-$i
cp $file/PPD temp$i.dat
awk 'FNR==1{f++}{a[f,FNR]=$2}
END
{for(x=1;x<=FNR;x++)
{for(y=1;y<ARGC;y++)
printf("%s ",a[y,x]);print ""} }'
temp$i.dat >> 123.dat
done
Now 123.dat only has the date of the last PPD in Case-N
I know I can use join(I used that command before) if every PPD file has at least one column the same, but it turns out to be extremely slow if I have lots of Case folders
Maybe
eval paste $(printf ' <(cut -f2 %s)' Case-*/PPD)
There is probably a limit to how many process substitutions you can perform in one go. I did this with 20 columns and it was fine. Process substitutions are a Bash feature, so not portable to other Bourne-compatible shells in general.
The wildcard will be expanded in alphabetical order. If you want the cases in numerical order, maybe use case-[1-9] case-[1-9][0-9] case-[1-9][0-9][0-9] to force the expansion to get the single digits first, then the double digits, etc.
The interaction between the outer shell script and inner awk invocation aren't working the way you expect.
Every time through the loop, the shell script calls awk a new time, which means that f will be unset, and then that first clause will set it to 1. It will never become 2. That is, you are starting a new awk process for each iteration through the outer loop, and awk is starting from scratch each time.
There are other ways to structure your code, but as a minimal tweak, you can pass in the number $i to the awk invocation using the -v option, e.g. awk -v i="$i" ....
Note that there are better ways to structure your overall solution, as other answerers have already suggested; I meant this response to be an answer the question, "Why doesn't this work?" and not "Please rewrite this code."
The below AWK program can help you.
#!/usr/bin/awk -f
BEGIN {
# Defaults
nrecord=1
nfiles=0
}
BEGINFILE {
# Check if the input file is accessible,
# if not skip the file and print error.
if (ERRNO != "") {
print("Error: ",FILENAME, ERRNO)
nextfile
}
}
{
# Check if the file is accessed for the first time
# if so then increment nfiles. This is to keep count of
# number of files processed.
if ( FNR == 1 ) {
nfiles++
} else if (FNR > nrecord) {
# Fetching the maximum size of the record processed so far.
nrecord=FNR
}
# Fetch the second column from the file.
array[nfiles,FNR]=$2
}
END {
# Iterate through the array and print the records.
for (i=1; i<=nrecord; i++) {
for (j=1; j<=nfiles; j++) {
printf("%5s", array[j,i])
}
print ""
}
}
Output:
$ ./get.awk Case-*/PPD
1 11 21
2 12 22
3 13 23
4 14 24
5 15 25
6 16 26
7 17 27
8 18 28
9 19 29
10 20 30
Here the Case*/PPD expands to Case-1/PPD, Case-2/PPD, Case-3/PPD and so on. Below are the source files for which the output was generated.
$ cat Case-1/PPD
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
7 7 7 7
8 8 8 8
9 9 9 9
10 10 10 10
$ cat Case-2/PPD
11 11 11 11
12 12 12 12
13 13 13 13
14 14 14 14
15 15 15 15
16 16 16 16
17 17 17 17
18 18 18 18
19 19 19 19
20 20 20 20
$ cat Case-3/PPD
21 21 21 21
22 22 22 22
23 23 23 23
24 24 24 24
25 25 25 25
26 26 26 26
27 27 27 27
28 28 28 28
29 29 29 29
30 30 30 30

Bash random number generator where number is not in array

I am attempting to create a random number generator that generates a number between 1 and 99 but not any number that has already been generated.
In the script array1 contains the numbers already generated. To make it easier to test I have reduced the random number range to 0 - 14 and manually created an array.
I am quite new to bash scripting and am picking it up with a couple of books and the internet.
I have tried a mixture of ideas, the one that seems to make most sense is
array1=( 1 2 3 6 7 8 9 10 11 12 13 )
func1() {
for var in "${array1[#]}"
do
echo $var
done
}
rnd=$[ $RANDOM % 14 ]
until [ $rnd != func1 ]
do
rnd=$[ $RANDOM % 14 ]
done
echo $rnd
however I know the problem is on line 9 the shell sees the following code:
until [ $rnd != 1 2 3 6 7 8 9 10 11 12 13 ]
I know that the solution is that line 9 needs to be:
until [ $rnd != 1 ] && [ $rnd != 2 ] && [ $rnd != 3 ] && ...
I just don't know how to make this happen automatically from the array. The array does vary in length depending on how many numbers have been generated.
Any help will be greatly appreciated!
This is something that I found difficulty doing in bash. The approach I came up with is to have func1() return true or false and modify the array to remove the number that has been picked.
array=( {1..15} )
func1() {
local pick="$1"
found=1
total=${#array[#]}
for ((i=0;i<total;i++)); do
if (( pick == ${array[i]} )); then
echo $pick
array=( ${array[#]:0:i} ${array[#]:((i + 1)):$total})
found=0
break
fi
done
return $found
}
numbers=3
for ((x=0;x<numbers;x++)); do
until func1 $(( $RANDOM % ( ${#array[#]} ) )); do
continue
done
done
As noted in one of the comments, using the Knuth Shuffle is an excellent way to do this
#!/bin/bash
shuffle() {
local i tmp size max rand
# Code from http://mywiki.wooledge.org/BashFAQ/026
# $RANDOM % (i+1) is biased because of the limited range of $RANDOM
# Compensate by using a range which is a multiple of the array size.
size=${#array[*]}
max=$(( 32768 / size * size ))
for ((i=size-1; i>0; i--)); do
while (( (rand=$RANDOM) >= max )); do :; done
rand=$(( rand % (i+1) ))
tmp=${array[i]} array[i]=${array[rand]} array[rand]=$tmp
done
}
# Fill an array with values 1 to 99
array=({1..99});
# Shuffle the array at random
shuffle
# Echo shuffled array
echo ${array[#]}
Output
$ ./knuth
58 78 6 37 84 79 81 43 50 25 49 56 99 41 26 15 86 11 96 90 76 46 92 70 87 27 33 91 1 2 73 97 65 69 42 32 39 67 72 52 36 64 24 88 60 35 83 89 66 30 4 53 57 28 75 48 40 74 18 23 45 61 20 31 21 16 68 80 62 8 98 14 7 19 47 55 22 85 59 17 77 10 63 93 51 54 95 82 94 9 44 38 13 71 34 29 5 3 12
You can also use the -R switch to sort, if your version of sort supports it:
for x in {1..99} ; do echo $x ; done | sort -R

Resources