How to create a random array of non-repeating numbers in php

PHPz
Release: 2023-04-18 14:30:33
Original
770 people have browsed it

PHP is a widely used server-side scripting language that is widely used when developing web applications. A common task is to create a random array of non-repeating numbers. This article will show you how to create such an array using PHP.

Before this, you need to understand three functions: rand(), array_push(), and in_array(). The rand() function is used to generate pseudo-random numbers, the array_push() function is used to add elements to the end of the array, and the in_array() function is used to check whether the element already exists in the array.

The following are two ways to create an array of random non-repeating numbers:

Method 1: Use while loop and in_array() function

First, create an empty array to Holds the generated number. Then use a while loop to generate the numbers and add each number to the array. But before adding the number to the array, check if it already exists in the array using the in_array() function. If the number already exists, the loop continues generating new numbers until a new number is found.

The following is an example code for creating an array of random non-repeating numbers using a while loop and the in_array() function:

$numbers = array();
while (count($numbers) < 10) {
  $rand = rand(1, 20);
  if (!in_array($rand, $numbers)) {
    array_push($numbers, $rand);
  }
}
print_r($numbers);
Copy after login

In this example, we create an array containing 10 elements. In each loop, we use the rand() function to generate a pseudo-random number between 1-20 and use the in_array() function to check if it already exists in the array. If not, we use the array_push() function to add the number to the end of the array. When the length of the array reaches 10, the loop terminates and the contents of the array are printed.

Method 2: Use a for loop and the shuffle() function

The second method is to use a for loop to generate a series of numbers and use the shuffle() function to shuffle them. In this example, we use the range() function to generate an array containing a series of numbers, and then use the shuffle() function to shuffle the elements in the array. This method does not require the use of the in_array() function because there are no duplicate numbers in the initial array.

The following is an example code for using a for loop and the shuffle() function to create an array of random non-repeating numbers:

$numbers = range(1, 20);
shuffle($numbers);
$numbers = array_slice($numbers, 0, 10);
print_r($numbers);
Copy after login

In this example, we use the range() function to generate a range between 1-20 array of numbers. Then use the shuffle() function to shuffle the elements in the array. Finally, we use the array_slice() function to select the first 10 elements from the array as an array of random non-repeating numbers and print the contents of the array.

Summary

This article shows two ways to create an array of random non-repeating numbers using PHP. The first method uses the in_array() function in a while loop to check if the number already exists in the array and add the non-duplicate numbers to the array. The second method uses a for loop to generate a series of numbers, then uses the shuffle() function to disrupt the order of the numbers, and select the first 10 elements from the array as a random array of non-repeating numbers. Both methods can achieve the purpose of randomly generating unique numbers, and which method to use depends on your personal preference and the requirements of your application.

The above is the detailed content of How to create a random array of non-repeating 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