Use Rand () in MySQL to generate random numbers with random numbers and random name strings.

Source: Internet
Author: User
Use Rand () in MySQL to generate random numbers with random numbers and random name strings.

Use Rand () in MySQL to generate random numbers with random numbers and random name strings.

Preface:
The RAND function returns a random floating point value ranging from 0 to 1.0. If an integer parameter N is specified, it is used as a seed value.

1. A friend's question is described as follows:
(1) There is a user table with id, real user name, and age.
A stored procedure is used to insert 100 data records into the table, where the age is random.
Name
Surname: 'zhao Qian Sun Li Zhou Wu Zheng Wang ';, randomly retrieve one
Name: 'character string'. A random word or two words are taken.
Requirement: random names cannot be repeated consecutively.

(2), then the code I wrote is as follows:
-- Create a user table in the project
Create table user1 (
Id int not null primary key auto_increment,
Name varchar (3) not null,
Age tinyint not null
) Charset utf8;

-- Create a stored procedure
Delimiter $
Create procedure pro3 (num int)
Begin
Declare fname char (1 );
Declare name1 char (1 );
Declare name2 char (1 );
Declare fullname varchar (3 );
Declare age int;
Declare I int default 1;

While I <= num do
Set fname = substring ('zhao Qian Sun Li Zhou Wu Zheng Wang ', floor (1 + 8 * rand (), 1); -- surname
Set name1 = substring ('August 5, 1234, 80 or 90, A, B, and C ding', floor (1 + 14 * rand (), 1); -- extract a word
Set name2 = substring ('february 5, 1234, 80 or 90, A, B, and C ding', floor (1 + 14 * rand (), 1); -- extract a word

If round (rand () = 0 then -- create a local variable fullname to indicate the full name, and randomly determine whether it is two or three words.
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 (); -- a random age of 20-50
Insert into user1 values (null, fullname, age); -- insert data
Set I = I + 1;
End while;
End
$
Delimiter;

(3) execution result
Call pro3 (100); -- Execution Process
The result is unexpected. Many Consecutive names are displayed. The execution result is as follows:
Mysql> select * from user1;
+ ----- + -------- + ----- +
| Id | name | age |
+ ----- + -------- + ----- +
| 1 | Zhao San | 28 |
| 2 | sun Yisi | 50 |
| 3 | Zheng dingjiu | 42 |
| 4 | wang ba | 47 |
| 5 | | 48 |
| 6 | Li Wu | 26 |
| 7 | Li Wu | 41 |
| 8 | Zheng ersan | 22 |
| 9 | sun qi6 | 24 |
| 10 | sun San | 21 |
| 11 | Qian Jia 2 | 26 |
| 12 | sun Bing | 42 |
| 13 | sun Bing | 37 |
| 14 | Zhao luiding | 24 |
| 15 | Zhao liuding | 40 |
| 16 | Wang 17th | 28 |
| 17 | sun JIU | 22 |
| 18 | Li 'er | 23 |
| 19 | LI Shi | 23 |
| 20 | Zhao dingjia | 50 |
| 21 | Qian YIJIU | 49 |
| 22 | Qian YIJIU | 22 |
| 23 | Qian YIJIU | 30 |
| 24 | Qian YIJIU | 30 |
| 25 | Sun Wu | 46 |
| 26 | Wu thirty | 39 |
| 27 | Zhao qi'er | 50 |
| 28 | Zhao qi'er | 29 |
| 29 | sun jiajiu | 20 |
| 30 | Zheng yic10 | 35 |
| 31 | Monday | 50 |
| 32 | Wu jiaqi | 50 |
| 33 | Li Ding 10 | 47 |
| 34 | Li Ding 10 | 29 |
| 35 | Li Ding ten | 40 |
| 36 | Sun Wu | 47 |
| 37 | Zhao jiubing | 41 |
| 38 | Zheng sanba | 43 |
| 39 | Qian Qi | 37 |
| 40 | Qian eryi | 24 |
| 41 | Wang Qiwu | 45 |
| 42 | Li 14th | 24 |
| 43 | Li 14 | 28 |
| 44 | Sun Jia | 49 |
| 45 | Zheng Wu | 38 |
| 46 | Zhao Jia | 27 |
| 47 | Wang San | 39 |
| 48 | Wang San | 26 |
| 49 | Wang San | 22 |
| 50 | Wang San | 30 |
| 51 | Qian YIJIU | 44 |
| 52 | Zhao Si | 30 |
| 53 | Wang 19th | 50 |
| 54 | Wang 19th | 34 |
| 55 | Wang 19 | 40 |
| 56 | Zhou bingjia | 20 |
| 57 | Sun shi | 47 |
| 58 | Wang Bing | 32 |
| 59 | Wang Liu | 43 |
| 60 | Wu Sanjia | 29 |
| 61 | Wang Yi | 26 |
| 62 | Zhao Jia | 46 |
| 63 | Zhao Jia | 23 |
| 64 | Wang Babo | 25 |
| 65 | Wang Babo | 30 |
| 66 | week B 6 | 50 |
| 67 | Wu WUBA | 21 |
| 68 | Sun Wu | 42 |
| 69 | Zhao bing6 | 48 |
| 70 | Wednesday | 48 |
| 71 | Wednesday | 47 |
| 72 | Li Yi | 46 |
| 73 | Li Yi | 40 |
| 74 | Wu 15th | 45 |
| 75 | Zhao 15th | 36 |
| 76 | Zhao 15th | 25 |
| 77 | LI Shi | 35 |
| 78 | Li Jia | 38 |
| 79 | week Sany | 33 |
| 80 | week Sany | 48 |
| 81 | qianliu6 | 33 |
| 82 | qianliu6 | 49 |
| 83 | sun | 36 |
| 84 | Wang dimethyl | 48 |
| 85 | Wang dimethyl | 39 |
| 86 | Wang Yi | 30 |
| 87 | Zheng San | 21 |
| 88 | Zhao erwu | 29 |
| 89 | Zhou Bing | 43 |
| 90 | Qian Qiyi | 35 |
| 91 | Qian Qiyi | 47 |
| 92 | Qian Qiyi | 38 |
| 93 | Sun Yi | 29 |
| 94 | week B | 32 |
| 95 | Zheng yic10 | 46 |
| 96 | Zheng yic10 | 37 |
| 97 | Wu Ba | 46 |
| 98 | Wu 18 | 42 |
| 99 | Wang Yisi | 30 |
| 100 | Wu 90 | 50 |

-------------------------------------- Split line --------------------------------------

Install MySQL in Ubuntu 14.04

MySQL authoritative guide (original book version 2nd) Clear Chinese scan PDF

Ubuntu 14.04 LTS install LNMP Nginx \ PHP5 (PHP-FPM) \ MySQL

Build a MySQL Master/Slave server in Ubuntu 14.04

Build a highly available distributed MySQL cluster using Ubuntu 12.04 LTS

Install MySQL5.6 and Python-MySQLdb in the source code of Ubuntu 12.04

MySQL-5.5.38 universal binary Installation

-------------------------------------- Split line --------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.