1. [root@www ~]# echo `expr ${RANDOM} % 14 + 1 `

10

[root@www ~]# 



2. [root@www ~]# echo $(( ${RANDOM} % 14 + 1 ))

4

[root@www ~]# 


3. [root@www ~]# echo $[ ${RANDOM} % 14 + 1 ]

13

[root@www ~]# 


4. [root@www ~]#  let m=${RANDOM}%14+1 ;echo $m

8

[root@www ~]# 


虽然Bash shell 有四种算术运算方法,但并不是每一种都是跨平台的,建议使用expr。
另外,我们在 script 中经常有加1操作,以下四法皆可:
m=$[ m + 1]
m=`expr $m + 1`
m=$(($m + 1))
let m=m+1


用mysql函数实现:

mysql> select rand();

+--------------------+

| rand()             |

+--------------------+

| 0.9146193386191682 |

+--------------------+

1 row in set (0.00 sec)


mysql> select ceil(100*rand()),ceil(100*rand());

+------------------+------------------+

| ceil(100*rand()) | ceil(100*rand()) |

+------------------+------------------+

|               31 |               78 |

+------------------+------------------+

1 row in set (0.00 sec)


mysql> select floor(100*rand()),floor(100*rand());

+-------------------+-------------------+

| floor(100*rand()) | floor(100*rand()) |

+-------------------+-------------------+

|                95 |                45 |

+-------------------+-------------------+

1 row in set (0.00 sec)


mysql> select mod(floor(100*rand()),15),floor(100*rand());

+---------------------------+-------------------+

| mod(floor(100*rand()),15) | floor(100*rand()) |

+---------------------------+-------------------+

|                        11 |                69 |

+---------------------------+-------------------+

1 row in set (0.00 sec)


mysql> select mod(floor(100*rand()),15),floor(100*rand()) % 15;

+---------------------------+------------------------+

| mod(floor(100*rand()),15) | floor(100*rand()) % 15 |

+---------------------------+------------------------+

|                         9 |                     11 |

+---------------------------+------------------------+

1 row in set (0.00 sec)


mysql> 


[root@192 ~]# /usr/bin/mysql -p3306 --socket=/data/3306/mysql.sock -e "select mod(ceil(100000*rand()),14) + 1 ;" | awk 'NR==2{print $0}' 

6

[root@192 ~]# /usr/bin/mysql -p3306 --socket=/data/3306/mysql.sock -e "select mod(ceil(100000*rand()),14) + 1 ;" | awk 'NR==2{print $0}'

14

[root@192 ~]# /usr/bin/mysql -p3306 --socket=/data/3306/mysql.sock -e "select mod(ceil(100000*rand()),14) + 1 ;" | awk 'NR==2{print $0}'

3

[root@192 ~]# /usr/bin/mysql -p3306 --socket=/data/3306/mysql.sock -e "select mod(ceil(100000*rand()),14) + 1 ;" | awk 'NR==2{print $0}'

8

[root@192 ~]# /usr/bin/mysql -p3306 --socket=/data/3306/mysql.sock -e "select mod(ceil(100000*rand()),14) + 1 ;" | awk 'NR==2{print $0}'

7

[root@192 ~]# 

完美,练习mysql的函数。