假设有这样的脚本
#!/bin/bash
array[0]="123"
array[1]="333"
array[2]="5566"
array[3]="135"
array[4]="68"
array[5]="45"
array[6]="78"
array[7]="32"
array[8]="190"
number()
{
size=${#array[@]}
index=$(($RANDOM % $size))
sleep 0.5s
echo Lucky number is ... ${array[$index]}
}
for i in {1..21}; do number; done
好的..我想在间隔中获取随机数,但也显示很多重复项
Lucky number is ... 135
Lucky number is ... 135
Lucky number is ... 5566
Lucky number is ... 78
Lucky number is ... 190
Lucky number is ... 333
Lucky number is ... 190
Lucky number is ... 135
Lucky number is ... 5566
Lucky number is ... 333
Lucky number is ... 45
Lucky number is ... 45
Lucky number is ... 68
Lucky number is ... 68
Lucky number is ... 333
Lucky number is ... 78
Lucky number is ... 78
Lucky number is ... 5566
Lucky number is ... 78
Lucky number is ... 333
Lucky number is ... 135
我想要这样的结果
Lucky number is ... 135
Lucky number is ... 5566
Lucky number is ... 78
Lucky number is ... 190
Lucky number is ... 333
Lucky number is ... 45
Lucky number is ... 68
这样我们就可以得到“幸运数字”而无需重复,独一无二有人知道怎么做吗?编辑脚本并将 21 减少到 9 没有改变,我什至得到了重复。
Lucky number is ... 135
Lucky number is ... 68
Lucky number is ... 45
Lucky number is ... 333
Lucky number is ... 78
Lucky number is ... 135
Lucky number is ... 135
Lucky number is ... 333
Lucky number is ... 32
答案1
将列表视为一副纸牌。 随机播放数字,然后一次一个地读出它们。
#!/bin/bash
array=("123" "333" "5566" "135" "68" "45" "78" "32" "190")
shuffle() {
array=($(shuf -e "${array[@]}"))
index=0
}
number() {
size=${#array[@]}
sleep 0.5s
if (( index >= size )) ; then
shuffle
fi
echo Lucky number is ... ${array[$index]}
index=$((index + 1))
}
for i in {1..21}; do number; done