1:

#!/bin/bash
for i in $(seq 1 1000)do
   ( Generating random numbers here , sorting  and outputting to file$i.txt ) &
   if (( $i % 10 == 0 )); then wait; fi # Limit to 10 concurrent subshells.
done
wait

2:

for ARG in  $*; do

    command $ARG &

    NPROC=$(($NPROC+1))

    if [ "$NPROC" -ge 4 ]; then

        wait

        NPROC=0

    fi

done