generate non-repeating random number sequences in

2019-07-22 10:23发布

问题:

I have been futzing about learning bash tonight and I have been trying to create a random number sequence that uses all the numbers of a range and uses each digit just once. So something like inputting the range of 1-5 will output something like 4-3-5-2-1 or 2-5-1-3-4 and so on. I am as stuck as can be on this one.

Thanks!

回答1:

the following command is not specific to bash, but it worked

seq 1 5 | shuf

a more bash specific with substrings

x=12345
for((i=5;i>0;i--));do
  ((r=RANDOM%i+1))
  echo ${x:r-1:1}
  x=${x:0:r-1}${x:r}
done