1

I've write the following code in bash, this code tends to print a matrix of 200x13 random numbers (we have 200 line printed with 13 numbers per line). I want this code to be more flexible such that one can change size of the matrix printed (number of line and number of number per line) without re-write the code

for (( k = 0; k < 200; k++  ))
do

    echo $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 ))

done

thanks for any help.

3 Answers 3

2

First, you want to change the printing of the columns to another for-loop:

for (( k = 0; k < 200; k++  ))
do
 for (( j = 0; j < 3; j++))
 do
    echo -n $(( $RANDOM - 16384 ))" "  # print your random number

 done

 echo     # print newline 
done

Then, you can parametrize your k max and j max:

rows=200   # set the number of rows
cols=3    # set the number of columns

for (( k = 0; k < $rows; k++  ))
do
   for (( j = 0; j < $cols; j++))
   do
       echo -n $(( $RANDOM - 16384 ))" "   

   done

   echo
done
Sign up to request clarification or add additional context in comments.

1 Comment

I think you mean parameterize rows/cols (of course, just semantics, as you're referring to the same thing.)
1

Put it in a shell function and use printf instead of echo.

printMatrix() {
  cols=${1:-13}
  rows=${2:-200}
  for (( i=0; i<$rows; i++ ))
  do
    for (( j=0; j<$cols; j++ ))
      printf "%13d" $(( $RANDOM - 16384))
    done
    echo
  done
}

printMatrix # will print 200x13 matrix
printMatrix 5 10  # with print 10x5 matrix

Since this is in a shell function, you can source the function definition and call it directly from the shell. If the function is in a file named shfuncs, then you can source it and run it like follows:

bash$ source shfuncs
bash$ printMatrix 3 2
11118        31           -9753        
-13700       -12008       -1779 
bash$

Comments

1

To do this simply, just add a nested loop to repeat the columns.

Also in bash you can do a for loop with brace expansion ...

for a in {1..200}
do # Rows...
    for b in {1..13}
    do # Columns...
        echo -n "$(( $RANDOM - 16384 )) " 
        # -n suppresses newlines
    done
    echo 
    # do newline at the end of the row.
done

If the script was going to become more complicated. I'd suggest abstracting the script into functions.

function printRandomNumber() { 
   seed={1:-16384}
   echo -n "$(( $RANDOM - $seed ))" 
}

function printRandomMatrix() {
    rows={1:-200} # default 200 if not set.
    cols={2:-13} # default 13 if not set.

    for a in {1..$rows}
    do
        for b in {1..$cols}
        do  
            printRandomNumber
        done
        echo 
    done
}

Then call it with

printRandomMatrix 
# print the default row/column matrix.

printRandomMatrix 10 10
# print a matrix of 10 by 10.

This way you can more easily maintain the script and adapt to changes, e.g. if you needed to format the output using printf as shown in D.Shawley's answer. Of course you may want a variety of possible visual formatting (left/right column alignment etc.) these can be achieved with a variety of tricks... More on shell text formatting

1 Comment

I suggest we clean up the noise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.