PHP generated 4 ways to share random numbers, arrays, 4 _php Tutorials

Source: Internet
Author: User
Tags php define shuffle

PHP generated 4 ways to share random numbers, arrays, 4 random numbers


Here are some ways to generate non-repeating random numbers, directly on the code bar

Copy the Code code as follows:
<?php
Define (' Random_max ', 100);
Define (' COUNT ', 10);

echo ' Max random num: '. Random_max, '; Result Count: '. COUNT, '
';

Invoke_entry (' Rand1 ');
Invoke_entry (' Rand2 ');
Invoke_entry (' Rand3 ');
Invoke_entry (' Rand4 ');

function Invoke_entry ($func _name) {
$time = new Time ();
$time->time_start ();
Call_user_func ($func _name);
echo $func _name ' time spend: ', $time->time_spend ();
Echo '
';
}
function Rand1 () {
$numbers = Range (1, Random_max);
Shuffle ($numbers); Randomly scrambled arrays
$result = Array_slice ($numbers, 1, COUNT);
return $result;
}
function Rand2 () {
$result = Array ();
while (count ($result) < count) {
$result [] = Mt_rand (1, Random_max); Mt_rand () is a better and faster random function than rand ()
$result = Array_unique ($result); Delete duplicate elements in an array
}
return $result;
}
function Rand3 () {
$result = Array ();
while (count ($result) < count) {
$_tmp = Mt_rand (1, Random_max);
if (!in_array ($_tmp, $result)) {//is allowed to be inserted when the same element does not exist in the array
$result [] = $_tmp;
}
}
return $result;
}
function Rand4 () {
$result = Array ();
while (count ($result) < count) {
$result [] = Mt_rand (1, Random_max);
$result = Array_flip (Array_flip ($result)); Array_flip to exchange the key and value of the array
}
return $result;
}
Class Time {
Private $_start;

Public Function Time_start () {
$this->_start = $this->microtime_float ();
}
Public Function Time_spend () {
return $this->microtime_float ()-$this->_start;
}
Private Function Microtime_float () {
List ($usec, $sec) = Explode ("", Microtime ());
return (float) $usec + (float) $sec);
}
}


?>

The fourth method is to turn over the method, using Array_flip () to flip the array's keys and values, using the PHP array feature, duplicate keys will overwrite, and then flip again, the same as the removal of duplicate values.
The above methods are simple examples, and some methods have limited scope of application.

Look at the efficiency of several methods:

With Array_unique () The performance is poor when the array is large, and of course shuffle () is affected by this.

http://www.bkjia.com/PHPjc/976033.html www.bkjia.com true http://www.bkjia.com/PHPjc/976033.html techarticle PHP generation of non-repeating random number, array of 4 ways to share, random number 4 below write several methods to generate non-repeating random numbers, directly on the Code bar copy code code as follows: PHP define ( ...

  • 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.