Two methods of generating random numbers in php example code output random ip_php Tips

Source: Internet
Author: User
Tags php example rand shuffle

Share the PHP generated random number of three methods, generate 1-10 between the repeat random number, PHP generation of random numbers of examples, need a friend reference.

How do I generate a repeat random number between 1-10 in PHP?

Example 1, using the shuffle function to generate random numbers.

<?php
$arr =range (1,10);
Shuffle ($arr);
foreach ($arr as $values)
{
  echo $values. " ";
}
? >

Example 2, using the Array_unique function to generate random numbers.

<?php
$arr =array ();
while (count ($arr) <10)
{
  $arr []=rand (1,10);
  $arr =array_unique ($arr);
}
echo Implode ("", $arr);
? >

Example 3, using the Array_flip function to generate random numbers, you can remove duplicate values.

<?php
$arr =array ();
$count 1=0;
$count = 0;
$return = Array ();
while ($count <) 
 {
  $return [] = Mt_rand (1);
  $return = Array_flip (Array_flip ($return));
  $count = count ($return);
 Www.jb51.net
foreach ($return as $value)
 {
  echo $value. " ";
 }
echo "<br/>";
$arr =array_values ($return);//Get the value of the array 
foreach ($arr as $key)
echo $key. " ";
? >

I am an ASP programmer, this is the first time to write PHP program, a little experience sent to share

<?php 
$ip 2id= round (rand (600000, 2550000)/10000)//first method, direct generation of 
$ip 3id= round (rand (600000, 2550000)/10000 ); 
$ip 4id= Round (rand (600000, 2550000)/10000); 
Here is the second method that randomly extracts 
$arr _1 = Array ("218") in the following data. 218 "," 66 "," 66 "," 218 "," 218 "," 60 "," 60 "," 202 "," 204 "," 66 "," 66 "," 66 ", "222", "221", "the" "," "", "" "," "," "," "," "," "," "218", "218", "," "," "," "," "," "," "122", "211"); 
$randarr = Mt_rand (0,count ($arr _1)-1); 
$ip 1id = $arr _1[$randarr]; 
echo $ip 1id; 
echo "."; 
echo $ip 2id; 
echo "."; 
echo $ip 3id; 
echo "."; 
echo $ip 4id; 
? >

The result of the example output is 218.28.131.182
This program is characterized by the generation of IP first field in the specified range, set a few are common in the country, which means that the generated IP address is mostly domestic
Core code:

<?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 "; 
? >

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.