generate non-repeating random number sequences in bash

▼魔方 西西 提交于 2019-12-11 03:54:42

问题


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


来源:https://stackoverflow.com/questions/12550372/generate-non-repeating-random-number-sequences-in-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!