英文:
how to generate hexadecimal number in range between 63 to 2048 using shell script
问题
echo $( NUMBER=0; FLOOR=63; RANGE=2048;
while [ $NUMBER -le $FLOOR ]; do \
NUMBER=$RANDOM; \
let "NUMBER %= $RANGE"; \
done; \
echo $NUMBER;)
英文:
echo $( NUMBER=0; FLOOR=63; RANGE=2048; \
while [ $NUMBER -le $FLOOR ]; do \
NUMBER=$RANDOM; \
let "NUMBER %= $RANGE"; \
done; \
echo $NUMBER;)
答案1
得分: 1
像这样:
randomintfromrange() { echo $(( ( RANDOM % ($2 - $1 +1 ) ) + $1 )); }
printf ''%x\n'' $(randomintfromrange 63 2048)
重复100次:
for ((;i++<100;)); do printf ''%x\n'' $(randomintfromrange 63 2048); done
英文:
Like this :
randomintfromrange() { echo $(( ( RANDOM % ($2 - $1 +1 ) ) + $1 )); }
printf '%x\n' $(randomintfromrange 63 2048)
To repeat 100 times:
for ((;i++<100;)); do printf '%x\n' $(randomintfromrange 63 2048); done
答案2
得分: 1
这个Bash函数可能会有所帮助:
# 用法:inrange num min max
# 示例:inrange 123456 63 2048 # => 387
inrange() {
printf "%X" $(( $2 + $1 % ($3 - $2 + 1) ))
}
然后
for ((i=0; i<5000; i++)); do
printf "%d\t%s\n" $i $(inrange $i 63 2048)
done
英文:
This bash function may help:
# usage: inrange num min max
# example: inrange 123456 63 2048 # => 387
inrange() {
printf "%X" $(( $2 + $1 % ($3 - $2 + 1) ))
}
and then
for ((i=0; i<5000; i++)); do
printf "%d\t%s\n" $i $(inrange $i 63 2048)
done
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论