Example 2, using array_uniqu"/> Example 2, using array_uniqu">

Home  >  Article  >  Backend Development  >  Generate random numbers. Example code of two methods for generating random numbers in PHP. Output random IP.

Generate random numbers. Example code of two methods for generating random numbers in PHP. Output random IP.

WBOY
WBOYOriginal
2016-07-29 08:44:441066browse

Share three methods of generating random numbers in PHP. Generate non-repeating random numbers between 1-10. Examples of PHP generating non-repeating random numbers. Friends in need can refer to them.

How to generate non-repeating random numbers between 1-10 using php?

Example 1, use shuffle function to generate random numbers.

<&#63;php
$arr=range(1,10);
shuffle($arr);
foreach($arr as $values)
{
  echo $values." ";
}
&#63;>

Example 2, use array_unique function to generate random numbers.

<&#63;php
$arr=array();
while(count($arr)<10)
{
  $arr[]=rand(1,10);
  $arr=array_unique($arr);
}
echo implode(" ",$arr);
&#63;>

Example 3, use array_flip function to generate random numbers, which can remove duplicate values.

<&#63;php
$arr=array();
$count1=0;
$count = 0;
$return = array();
while ($count < 10) 
 {
  $return[] = mt_rand(1, 10);
  $return = array_flip(array_flip($return));
  $count = count($return);
 } //www.jb51.net
foreach($return as $value)
 {
  echo $value." ";
 }
echo "<br/>";
$arr=array_values($return);// 获得数组的值 
foreach($arr as $key)
echo $key." ";
?>

I am an asp programmer. This is my first time writing a php program. I would like to share my experience

<&#63;php 
$ip2id= round(rand(600000, 2550000) / 10000); //第一种方法,直接生成 
$ip3id= round(rand(600000, 2550000) / 10000); 
$ip4id= round(rand(600000, 2550000) / 10000); 
//下面是第二种方法,在以下数据中随机抽取 
$arr_1 = array("218","218","66","66","218","218","60","60","202","204","66","66","66","59","61","60","222","221","66","59","60","60","66","218","218","62","63","64","66","66","122","211"); 
$randarr= mt_rand(0,count($arr_1)-1); 
$ip1id = $arr_1[$randarr]; 
echo $ip1id; 
echo "."; 
echo $ip2id; 
echo "."; 
echo $ip3id; 
echo "."; 
echo $ip4id; 
&#63;>

The output result of the example is 218.28.131.182
The characteristic of this program is that the first field of the generated IP is Within the specified range, the set ones are common domestic number ranges, which means that most of the generated IP addresses are domestic. Core code:

<&#63;php 
$arr_1 = array("http://66.249.89.99","http://66.249.89.104","http://74.125.71.105"); 
$randarr= mt_rand(0,count($arr_1)-1); 
$gip= $arr_1[$randarr]; 
echo $gip."$randarr"; 
&#63;>

The above introduces the example code of two methods of generating random numbers in PHP. Outputting random IP includes the content of generating random numbers. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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