Home > Backend Development > PHP Problem > How to generate non-repeating random numbers in php

How to generate non-repeating random numbers in php

PHPz
Release: 2023-04-24 13:37:07
Original
2361 people have browsed it

When performing data processing, we often need to use random numbers. In PHP, random numbers can be easily generated through the built-in function rand(). However, since the rand() function generates repeated numbers, in order to ensure that the generated random numbers are not repeated, it is necessary to use other methods.

In this article, we will introduce how to generate non-repeating random numbers through two methods.

Method 1: Use the shuffle() function

Using the shuffle() function in PHP, we can scramble the order of an array, and then use the array_pop() function to take out the last end of the array One element as a random number.

Sample code:

$numbers = range(1, 100);
shuffle($numbers);

$random_number = array_pop($numbers);
Copy after login

In the above code, we first use the range() function to generate an array of numbers from 1 to 100, and then use the shuffle() function to disrupt the order of the array. Finally, use the array_pop() function to take out the last element of the array as our random number.

Compared with the rand() function, this method does not generate numbers repeatedly. However, the size of the array affects the performance of generating random numbers. Therefore, this method is not recommended when a large number of random numbers need to be generated.

Method 2: Use the SplFixedArray class

After PHP 5.3, the SplFixedArray class was introduced, which provides us with a fixed-size array, that is, the array size cannot be changed. Therefore, this method performs better when dealing with large numbers of random numbers.

Sample code:

$numbers = new SplFixedArray(100);

for ($i = 0; $i < 100; $i++) {
    $numbers[$i] = $i + 1;
}

$numbers->shuffle();

$random_number = $numbers->top();
Copy after login

In the above code, we first use the SplFixedArray() function to generate a fixed-size array with a length of 100, and then use the for() loop to assign values ​​to the array. Then, use the shuffle() function to shuffle the order of the array, and finally use the top() function to take out the last element of the array as our random number.

Compared with method 1, this method is more efficient and memory-friendly.

Conclusion

In practical applications, we need to choose different methods to generate random numbers based on the amount of data and performance requirements. When the amount of data is small, you can use method one to generate random numbers; when the amount of data is large and the performance requirements are high, you can use method two.

The above is the detailed content of How to generate non-repeating random numbers in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template