求如何产生(1,230)的随机数

sh/bash/dash/ksh/zsh等Shell脚本
回复
blue sea
帖子: 198
注册时间: 2007-11-10 8:57

求如何产生(1,230)的随机数

#1

帖子 blue sea » 2009-06-12 10:38

求如何产生(1,230)的随机数
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 求如何产生(1,230)的随机数

#2

帖子 eexpress » 2009-06-12 11:03

$RANDOM%229
再加1?
● 鸣学
blue sea
帖子: 198
注册时间: 2007-11-10 8:57

Re: 求如何产生(1,230)的随机数

#3

帖子 blue sea » 2009-06-12 11:14

找到了是,

代码: 全选

rnumber=$((RANDOM%230+1))
推广一下有:

若求(x,y)的随机数rnmuber, 则有rnumber=$((RANDOM%(y+1-x)+x).

如(5,35)的随机数,则有rnumber=$((RANDOM%31+5).
blue sea
帖子: 198
注册时间: 2007-11-10 8:57

Re: 求如何产生(1,230)的随机数

#4

帖子 blue sea » 2009-06-12 12:03

在控制台一行行输就变化,写在脚本里得出的数值固定,怎么回事?
blue sea
帖子: 198
注册时间: 2007-11-10 8:57

Re: 求如何产生(1,230)的随机数

#5

帖子 blue sea » 2009-06-12 12:06

晕,不能用sh test.sh, 只能用bash test.sh, 区别在哪?
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: 求如何产生(1,230)的随机数

#6

帖子 aerofox » 2009-06-12 12:17

区别就在 sh 跟 bash 不同,或者是另一个 shell,或者虽是 bash,但它试图去兼容 Borne Shell。
blue sea
帖子: 198
注册时间: 2007-11-10 8:57

Re: 求如何产生(1,230)的随机数

#7

帖子 blue sea » 2009-06-12 12:24

明白了,下次不能都 用sh.
archangelwin
帖子: 74
注册时间: 2007-09-09 14:34

Re: 求如何产生(1,230)的随机数

#8

帖子 archangelwin » 2009-06-13 9:30

:em03 i学好了。谢谢
回复