MYSQL generates random numbers

A function to generate a random string:

set global log_bin_trust_function_creators=TRUE;

CREATE FUNCTION `rand_string`(n INT) RETURNS varchar(255) CHARSET latin1
BEGIN
DECLARE chars_str varchar(100) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
DECLARE return_str varchar(255) DEFAULT '' ;
DECLARE i INT DEFAULT 0;
WHILE i < n DO
SET return_str = concat(return_str,substring(chars_str , FLOOR(1 + RAND()*62 ),1));
SET i = i +1;
END WHILE;
RETURN return_str;
END

 

A stored procedure that generates random data based on the table structure:

CREATE  PROCEDURE `add_log`(IN n int)
BEGIN  
  DECLARE i INT DEFAULT 1;
    WHILE (i <= n ) DO
      INSERT into tb_fws_nat_log (date, time, src, spt, dst, dpt, proto, conv_ip, conv_port, result)
			VALUEs (concat('201',(floor(rand()*6)+1),'-',floor(rand()*12)+1,'-',floor(rand()*31),' ',floor(10+rand()*10),':',floor(10+rand()*49),':',floor(10+rand()*49)),
			FLOOR(RAND() * 99999999),FLOOR(RAND() * 4294967295),FLOOR(RAND() * 65535),FLOOR(RAND() * 4294967295),FLOOR(RAND() * 65535),
			rand_string(12),FLOOR(RAND() * 4294967295),FLOOR(RAND() * 65535),rand_string(8));
			set i=i+1;
    END WHILE;
END

 

Call the stored procedure to insert 1000 random pieces of data:

CALL add_log(1000);

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326644419&siteId=291194637