Mysql-php randomly generates 2 million records with many duplicates

Source: Internet
Author: User
Tags random seed
During mysql optimization, I created a temporary table to generate million data records. However, I do not understand that the generated records are repeated and there are many records, thank you for choosing {code ...} note: The execution time is too long. It must have exceeded the maximum execution time of php. The configuration file is modified... during mysql optimization, I created a temporary table to generate million data records. However, I don't understand that the generated records are repeated and there are still a lot of questions.
Thank you!

$conn=new mysqli("localhost","root","","test");$conn->query("set names utf8");function str_rand($num){    $str="abcdefghijkmnpqrstuvwxyz0123456789";    $return_str="";    for($i=0;$i<$num;$i++){        $return_str.=substr($str,rand(0,33),1);    }    return $return_str;}$sql="insert into news (id,title,author,keywords,descrition) values (?,?,?,?,?)";$mysqli_stmt=$conn->prepare($sql);for($i=1;$i<=2000000;$i++){    $title=str_rand(rand(3,30));    $author=str_rand(rand(3,20));    $keywords=str_rand(rand(10,100));    $descrition=str_rand(rand(30,255));    $mysqli_stmt->bind_param("issss",$i,$title,$author,$keywords,$descrition);    $res=$mysqli_stmt->execute();}$mysqli_stmt->close();$conn->close();

Note: The execution time is too long. It must have exceeded the maximum execution time of php. The configuration file has been modified, and the inserted data contains 100 records later, for example, if you query the title = 'xxx' field, you can query more than entries.

Reply content:

During mysql optimization, I created a temporary table to generate million data records. However, I don't understand that the generated records are repeated and there are still a lot of questions.
Thank you!

$conn=new mysqli("localhost","root","","test");$conn->query("set names utf8");function str_rand($num){    $str="abcdefghijkmnpqrstuvwxyz0123456789";    $return_str="";    for($i=0;$i<$num;$i++){        $return_str.=substr($str,rand(0,33),1);    }    return $return_str;}$sql="insert into news (id,title,author,keywords,descrition) values (?,?,?,?,?)";$mysqli_stmt=$conn->prepare($sql);for($i=1;$i<=2000000;$i++){    $title=str_rand(rand(3,30));    $author=str_rand(rand(3,20));    $keywords=str_rand(rand(10,100));    $descrition=str_rand(rand(30,255));    $mysqli_stmt->bind_param("issss",$i,$title,$author,$keywords,$descrition);    $res=$mysqli_stmt->execute();}$mysqli_stmt->close();$conn->close();

Note: The execution time is too long. It must have exceeded the maximum execution time of php. The configuration file has been modified, and the inserted data contains 100 records later, for example, if you query the title = 'xxx' field, you can query more than entries.

PHPOfrand()The pseudo-random algorithm is used to produce regular data.
A random distribution chart andPHP rand()The data distribution chart is displayed at a glance.
True random: results are evenly distributed.

PHP rand(): Apparently striped (regular)

This problem does not exist when mt_rand () is used.

The random_int () of php7 is better than mt_rand ().

To generate a non-repeated random series, a continuous series is usually generated in advance and saved to a list. Then obtain the index using the pseudo-random algorithm to retrieve the value. Alternatively, write a shuffling method to disrupt the continuous sequence, use the pseudo-random algorithm to control the shuffling, and then read the data in sequence.
Of course, the most important thing about pseudo-random algorithms is seed selection. The most common source of seed is the current timestamp. There can also be other ways to read many articles about random seed on the Internet.

REFERENCE The mt_rand () function in the PHP manual:

Many old libc random number generators have some uncertain and unknown features and are very slow. The rand () function of PHP uses the libc random number generator by default. The mt_rand () function is informal to replace it. This function uses known features in Mersenne Twister as a random number generator, which can generate random values at an average speed four times faster than the rand () provided by libc.

You need to replace rand () with mt_rand (). In addition, you can use GUID to generate unique data.

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.