mysql隨機數生成原理?
1) 產生0到1000間的隨機數
SELECT RAND() * 10000;
對應產生相應的整數
SELECT FLOOR(RAND() * 10000)
2) 使用MD5()產生32位隨機字符串
SELECT MD5(RAND() * 10000)
3) 產生500-1000間的整形
SELECT FLOOR( 500 + RAND() * (1000 - 500))
4) 假設某表如下結構
CREATE TABLE fact ( dim1 int, dim2 int, name varchar(20),
hash varchar(32),
measure1 double );
則可以自動產生適合其結構的大量隨機記錄 create table names(id int auto_increment primary key, name varchar(20));
insert into names (name) values ('Justin','Jerry','James','Josh','Julien');
select (select name from names where id = 1 + rand() * 4);
這里有一個表專門產生隨機的字符串;
INSERT INTO fact SELECT FLOOR(1+ rand()*9999), FLOOR(1 + rand()*499), (select name from names where id = 1 + rand() * 4), MD5(1+rand()*9999), rand() FROM fact;