Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generate non-repeating random number sequences in bash

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!

like image 399
user1691946 Avatar asked Nov 16 '25 17:11

user1691946


1 Answers

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
like image 183
Nahuel Fouilleul Avatar answered Nov 19 '25 09:11

Nahuel Fouilleul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!