前言:
RAND函数,返回在范围0到1.0内的随机浮点值。如果一个整数参数N被指定,它被用作种子值。
一,朋友问题描述如下:
通过一段存储过程来实现向表里插入100数据,其中年龄随机
(2),然后我写出的代码如下:
-- 创建用户表,在project里
(3),执行结果
call pro3(100); -- 执行过程
二,分析原因

所以存储过程改动如下:
DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS `pro3`$$
CREATE PROCEDURE `pro3`(num INT)
BEGIN
DECLARE fname CHAR(1);
DECLARE name1 CHAR(1);
DECLARE name2 CHAR(1);
DECLARE fullname VARCHAR(3);
DECLARE pre_fullname VARCHAR(3) DEFAULT \'\';
DECLARE age INT;
DECLARE i INT DEFAULT 1;
WHILE i <=num DO
SET fname = SUBSTRING(\'赵钱孙李周吴郑王\',FLOOR(1+8*RAND()),1);
SET name1 = SUBSTRING(\'一二三四五六七八九十甲乙丙丁\',ROUND(1+14*RAND()),1);
SET name2 = SUBSTRING(\'一二三四五六七八九十甲乙丙丁\',ROUND(1+14*RAND()),1);

IF ROUND(RAND())=0 THEN
SET fullname = CONCAT(fname,name1);
END IF;
IF ROUND(RAND())=1 THEN
SET fullname = CONCAT(fname,name1,name2);
END IF;

SET age = FLOOR(20+31*RAND());
/* 给临时变量赋值,看最新的一条记录的name值 */
SELECT NAME INTO pre_fullname FROM user1 ORDER BY id DESC LIMIT 1;
/* 开始判断,如果刚赋值的name跟上一次赋值的name值一样,就跳过,i保持不变,继续取值;否则i+1,继续循环取值*/
IF fullname=pre_fullname AND i>1 THEN
SET i=i;
ELSEIF fullname IS NULL THEN
SET i=i;
ELSE
INSERT INTO user1 VALUES(NULL,fullname,age);
SET i = i + 1;
END IF ;
END WHILE;
END$$
DELIMITER
三,执行改动后的存储过程,查看效果
> DELIMITER $$
mysql> USE `test`$$
Database changed
mysql> DROP PROCEDURE IF EXISTS `pro3`$$
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE PROCEDURE `pro3`(num INT)
-> BEGIN
-> DECLARE fname CHAR(1);
-> DECLARE name1 CHAR(1);
-> DECLARE name2 CHAR(1);
-> DECLARE fullname VARCHAR(3);
-> DECLARE pre_fullname VARCHAR(3) DEFAULT \'\';
-> DECLARE age INT;
-> DECLARE i INT DEFAULT 1;
-> WHILE i <=num DO
-> SET fname = SUBSTRING(\'赵钱孙李周吴郑王\',FLOOR(1+8*RAND()),1);
-> SET name1 = SUBSTRING(\'一二三四五六七八九十甲乙丙丁\',ROUND(1+14*RAND()),1);
-> SET name2 = SUBSTRING(\'一二三四五六七八九十甲乙丙丁\',ROUND(1+14*RAND()),1);
->
-> IF ROUND(RAND())=0 THEN
-> SET fullname = CONCAT(fname,name1);
-> END IF;
-> IF ROUND(RAND())=1 THEN
-> SET fullname = CONCAT(fname,name1,name2);
-> END IF;
->
-> SET age = FLOOR(20+31*RAND());
-> /* 给临时变量赋值,看最新的一条记录的name值 */
-> SELECT NAME INTO pre_fullname FROM user1 ORDER BY id DESC LIMIT 1;
-> /* 开始判断,如果刚赋值的name跟上一次赋值的name值一样,就跳过,i保持不变,继续取值;否则i+1,继续循环取值*/
-> IF fullname=pre_fullname AND i>1 THEN
->
Display all 799 possibilities? (y or n)
-> SET i=i;
-> ELSEIF fullname IS NULL THEN
->
Display all 799 possibilities? (y or n)
-> SET i=i;
Display all 799 possibilities? (y or n)
-> SET i=i;
-> ELSE
->
Display all 799 possibilities? (y or n)
-> INSERT INTO user1 VALUES(NULL,fullname,age);
->
Display all 799 possibilities? (y or n)
-> SET i = i + 1;
-> END IF ;
-> END WHILE;
-> END$$
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;
mysql>
mysql> truncate table user1;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from user1;
Empty set (0.00 sec)

mysql> call pro3(100);
Query OK, 1 row affected (0.11 sec)

mysql> select * from user1;
+-----+-----------+-----+
| id | name | age |
+-----+-----------+-----+
| 1 | 吴甲九 | 32 |
| 2 | 赵丙 | 49 |
| 3 | 钱二 | 40 |
| 4 | 郑二丙 | 34 |
| 5 | 孙四三 | 39 |
| 6 | 钱五十 | 39 |
| 7 | 赵五 | 23 |
| 8 | 郑九 | 22 |
| 9 | 钱九七 | 47 |
| 10 | 郑七乙 | 39 |
| 11 | 孙丙二 | 44 |
| 12 | 吴三九 | 30 |
| 13 | 钱九四 | 28 |
| 14 | 王十九 | 49 |
| 15 | 周丙乙 | 43 |
| 16 | 王十 | 33 |
| 17 | 钱乙七 | 36 |
| 18 | 赵七四 | 32 |
| 19 | 郑八二 | 31 |
| 20 | 赵二 | 35 |
| 21 | 赵丙 | 26 |
| 22 | 郑甲丁 | 38 |
| 23 | 周丙九 | 50 |
| 24 | 李丙五 | 27 |
| 25 | 李五三 | 37 |
| 26 | 孙丙六 | 46 |
| 27 | 钱八 | 37 |
| 28 | 周七六 | 40 |
| 29 | 李一一 | 45 |
| 30 | 钱十七 | 22 |
| 31 | 吴七一 | 49 |
| 32 | 吴丙六 | 39 |
| 33 | 吴四四 | 50 |
| 34 | 郑八丙 | 28 |
| 35 | 吴五七 | 26 |
| 36 | 吴甲 | 46 |
| 37 | 钱四 | 39 |
| 38 | 王丙九 | 24 |
| 39 | 王八八 | 38 |
| 40 | 吴乙丙 | 35 |
| 41 | 吴丙七 | 39 |
| 42 | 周甲丁 | 31 |
| 43 | 钱二甲 | 25 |
| 44 | 钱五丁 | 32 |
| 45 | 孙四二 | 28 |
| 46 | 李七丙 | 37 |
| 47 | 钱七七 | 46 |
| 48 | 郑乙四 | 33 |
| 49 | 钱甲丁 | 34 |
| 50 | 王五 | 37 |
| 51 | 吴一 | 44 |
| 52 | 王二十 | 33 |
| 53 | 郑四 | 41 |
| 54 | 钱九八 | 29 |
| 55 | 李十十 | 24 |
| 56 | 钱六七 | 44 |
| 57 | 王二 | 38 |
| 58 | 周五 | 36 |
| 59 | 吴九 | 44 |
| 60 | 赵丙 | 38 |
| 61 | 李五五 | 20 |
| 62 | 王八乙 | 33 |
| 63 | 王丙八 | 49 |
| 64 | 王九六 | 38 |
| 65 | 钱七 | 45 |
| 66 | 赵二二 | 36 |
| 67 | 钱五乙 | 40 |
| 68 | 孙甲七 | 43 |
| 69 | 郑甲五 | 47 |
| 70 | 赵丙 | 20 |
| 71 | 郑四 | 43 |
| 72 | 孙丙五 | 24 |
| 73 | 孙三 | 33 |
| 74 | 赵四乙 | 31 |
| 75 | 李 | 44 |
| 76 | 吴九丁 | 43 |
| 77 | 郑甲一 | 33 |
| 78 | 李三五 | 37 |
| 79 | 王二 | 31 |
| 80 | 赵七丙 | 35 |
| 81 | 吴三十 | 22 |
| 82 | 李二 | 50 |
| 83 | 李八 | 49 |
| 84 | 王一 | 27 |
| 85 | 王三 | 22 |
| 86 | 周五 | 45 |
| 87 | 郑八 | 44 |
| 88 | 郑甲二 | 48 |
| 89 | 赵乙 | 37 |
| 90 | 周四五 | 30 |
| 91 | 周二七 | 41 |
| 92 | 孙四 | 21 |
| 93 | 周丙七 | 24 |
| 94 | 孙丁乙 | 32 |
| 95 | 周九一 | 20 |
| 96 | 周九 | 32 |
| 97 | 赵二一 | 26 |
| 98 | 周丁五 | 47 |
| 99 | 孙三 | 38 |
| 100 | 吴十四 | 29 |
+-----+-----------+-----+
100 rows in set (0.00 sec)
mysql>从中看到已经连续100个随机数中,没有出现连续重复的姓名和年龄。
四,RAND()与RAND(N)区别
每个种子产生的随机数序列是不同的,如果传入N参数执行RAND(N),则每次查询到的随机数都是固定的,如果没有传入N参数,则每次都是随机的值。如下所示:
mysql> SELECTRAND(),RAND(5);
+--------------------+---------------------+
| RAND() | RAND(5) |
+--------------------+---------------------+
| 0.7946587333405938 | 0.40613597483014313 |
+--------------------+---------------------+
1 row in set (0.00 sec)

mysql> SELECT RAND(),RAND(5);
+---------------------+---------------------+
| RAND() | RAND(5) |
+---------------------+---------------------+
| 0.12910866749389904 | 0.40613597483014313 |
+---------------------+---------------------+
1 row in set (0.00 sec)

mysql> SELECT RAND(),RAND(5);
+-------------------+---------------------+
| RAND() | RAND(5) |
+-------------------+---------------------+
| 0.261567168181359 | 0.40613597483014313 |
+-------------------+---------------------+
1 row in set (0.00 sec)

mysql> SELECT RAND(),RAND(5);
+--------------------+---------------------+
| RAND() | RAND(5) |
+--------------------+---------------------+
| 0.9205098691587428 | 0.40613597483014313 |
+--------------------+---------------------+
1 row in set (0.00 sec)

mysql> SELECT RAND(),RAND(5);
+--------------------+---------------------+
| RAND() | RAND(5) |
+--------------------+---------------------+
| 0.8178478719832821 | 0.40613597483014313 |
+--------------------+---------------------+
1 row in set (0.00 sec)

mysql>