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:
- Don't use backticks for command substitution, use
$()
instead. See Have backticks (i.e. cmd
) in *sh shells been deprecated?
- You don't need command substitution or
expr
to store $RANDOM
into another variable, just use variable assignment (var="$RANDOM"
).
- 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)