0
#!/bin/ksh
for i in `seq 1 10` ; do
  n=`expr $RANDOM`;
  (
    for (( k=1; k<=10; k++ ))
    do
      echo -n "$k"
    done
  )
  echo   "$k, $n"
done

I have written this script, but not getting exact output

12345678910, 17517
12345678910, 3695
12345678910, 8841
12345678910, 11553
12345678910, 2019
12345678910, 3503
12345678910, 25789
12345678910, 24825
12345678910, 8039
12345678910, 30532

while i need it as 1-10 with side by side random number

cas
  • 78,579
harish
  • 11
  • 2
    It is unclear what output you are expecting. – Kusalananda Aug 01 '21 at 10:45
  • 1
    Please [edit] your question and give us an example of the output you are expecting. Your script is doing exactly what it is supposed to, so we can't guess what you want from your code: your code will produce what you have shown. – terdon Aug 01 '21 at 15:14

2 Answers2

2

Perhaps something like this, using an array r to store 10 random numbers for each iteration of the i loop:

#!/bin/ksh
for i in $(seq 1 10) ; do
  r=()
  for ((k=1; k<=10; k++)); do
    r+=($RANDOM)
  done
  echo "$i, ${r[@]}"
done

output would be:

$ ./seq.ksh 
1, 28324 21310 5112 23165 31527 5779 17343 31716 11594 9769
2, 7205 15089 16436 10276 9784 18724 18137 20754 7878 11092
3, 12087 19351 25813 77 25109 12122 14299 287 21751 9714
4, 29046 17308 31024 1391 7705 29784 7170 25049 28732 18765
5, 2051 3169 1086 18488 13445 10871 4444 31583 31625 12323
6, 9907 10945 31675 2952 11023 24016 15075 25322 24303 4059
7, 2268 20582 21367 525 21973 29073 30309 29143 21355 26273
8, 15140 23406 29443 16227 9126 10121 27098 13571 8936 25956
9, 25894 18844 4134 24801 21797 15158 16049 4104 7712 7585
10, 8163 9981 28167 29531 10506 17372 25836 8047 13748 14423

Update: after seeing your comment

#!/bin/ksh
for i in $(seq 1 10) ; do
  printf "%2i, %5i\n" "$i" "$RANDOM"
  # or if you prefer echo to printf, just:
  # echo "$i,$RANDOM"
done

output:


 1, 23984
 2,  3987
 3, 31979
 4, 18977
 5,  1492
 6, 30008
 7, 17715
 8, 14603
 9, 20650
10,  9891

Notes:

  1. Don't use backticks for command substitution, use $() instead. See Have backticks (i.e. cmd) in *sh shells been deprecated?
  2. You don't need command substitution or expr to store $RANDOM into another variable, just use variable assignment (var="$RANDOM").
  3. The output could be formatted a bit better using printf rather than echo. You can even use printf to write a perl-like join function (see the join_by function in How can I join elements of an array in Bash? for a good example - this function would work in ksh as well as bash)
cas
  • 78,579
  • not very important, but somehow the fact that echo "$i, ${r[@]}" results in the arguments 1, 28324, 21310, 5112..., seems a bit messy to me, even if the result prints fine with echo. echo "$i, ${r[*]}" (assuming default IFS) would seem cleaner considering that joining the elements of r to one string is what we want to do here. – ilkkachu Aug 01 '21 at 15:28
  • [*] probably would have been better but i originally wrote the first version as a printf, but changed it at the last minute to echo. – cas Aug 01 '21 at 15:45
0

Assuming that you have a shell that understands process substitutions:

$ paste <( seq 10 ) <( shuf -n 10 -r -i 0-100 )
1       50
2       59
3       54
4       89
5       73
6       6
7       46
8       35
9       32
10      55

This creates two tab-delimited columns of numbers using paste.

The first column is simply the numbers from 1 to 10 outputted by seq (a GNU tool).

The second column consists of random number between 0 and 100 (possibly repeated due to -r), generated by shuf (another GNU tool).

Would you want another delimiter than tab, use -d with paste:

$ paste -d , <( seq 10 ) <( shuf -n 10 -r -i 0-100 )
1,37
2,56
3,33
4,48
5,71
6,45
7,52
8,52
9,99
10,80
Kusalananda
  • 333,661