


PHP generates multiple non-repeating random numbers example program_PHP tutorial
Generating random data in php can be achieved directly by using mt_rand. If we want to generate non-repeating random numbers, we can use the unique_rand function. Let me summarize the commonly used methods.
The code is as follows:
The code is as follows | Copy code | ||||||||
$numbers = range (1,100); //shuffle will disrupt the order of the array shuffle ($numbers); //array_slice takes a certain segment of the array $no=6; $result = array_slice($numbers,0,$no); for ($i=0;$i echo $result[$i]." "; } print_r($result); ?>
//range is to list 1 to 42 into an array $numbers = range (1,42); //shuffle will disrupt the order of the array shuffle ($numbers); //array_slice takes a certain segment of the array $result = array_slice($numbers,0,3); print_r($result);
|
Method 2
The code is as follows | Copy code |
$numbers = range (1,20); |
代码如下 | 复制代码 |
function NoRand($begin=0,$end=20,$limit=5){ |
Use PHP to randomly generate 5 unique values between 1-20. How to do it
代码如下 | 复制代码 |
$tmp=array(); |
The code is as follows | Copy code |
function NoRand($begin=0,$end=20,$limit=5){ $rand_array=range($begin,$end); shuffle($rand_array);//Call the ready-made array random arrangement function return array_slice($rand_array,0,$limit); //Intercept the first $limit items } print_r(NoRand()); ?> |
The code is as follows | Copy code |
$tmp=array(); while(count($tmp) $tmp[]=mt_rand(1,20); $tmp=array_unique($tmp); } print join(',',$tmp); ?> |
The above is all talk on paper, now comes the reality. The requirements are as follows
There are 25 works for voting. You need to select 16 works in one vote. A single work can only be selected once in one vote. A programmer made a mistake earlier and forgot to store the votes in the database. The voting sequences generated by 200 users were empty. So how do you fill this gap?
Of course, report the situation to your superiors. But what we are discussing here is technology, which requires generating 16 non-repeating random numbers between 1-25 to fill in. How to design the function specifically? Store random numbers in an array, and then remove duplicate values in the array to generate a certain number of non-repeating random numbers
The code is as follows | Copy code | ||||||||
* $num: Specify the generated quantity $count = 0; $return = array();While ($count $return[] = mt_rand($min, $max); $return = array_flip(array_flip($return)); $count = count($return); }Shuffle($return); Return $return;
|
The code is as follows | Copy code |
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => a [11] => b [12] => c [13] => d [14] => e [15] => f [16] => g [17] => h [18] => i [19] => j [20] => k [21] => l [22] => m [23] => n [24] => o [25] => p [26] => q [27] => r [28] => s [29] => t [30] => u [31] => v [32] => w [33] => x [34] => y [35] => z ) |
Then randomly generate a number between 0-35 as the index, which is actually to randomly pick out a number from the above array as the first character in the variable $result. This random index will then be assigned as the last one in the array, and it will not participate in the next round of random selection.
The code is as follows
|
Copy code | ||||
function getOptions()
{
$options = array();
$result = array();
for($i=48; $i {
array_push($options,chr($i));
}
for($i=65; $i {
$j = 32;
$small = $i + $j;
array_push($options,chr($small));
}
return $options;
}
/*
$e = getOptions();
for($j=0; $j {
echo $e[$j];
}
*/
$len = 10;
// Randomly generate array index to achieve random numbers
for($j=0; $j {
$result = "";
$options = getOptions();
$lastIndex = 35;
while (strlen($result) {
// Randomly pick one from 0 to 35 as the index
$index = rand(0,$lastIndex);
//Assign random number to variable $chr
$chr = $options[$index];
// Random number as part of $result
$result .= $chr;
$lastIndex = $lastIndex-1;
//The last index will not participate in the next random draw
$options[$index] = $options[$lastIndex];
}
echo $result."n";
}
?>
http: //www.bkjia.com/PHPjc/633165.html

To protect the application from session-related XSS attacks, the following measures are required: 1. Set the HttpOnly and Secure flags to protect the session cookies. 2. Export codes for all user inputs. 3. Implement content security policy (CSP) to limit script sources. Through these policies, session-related XSS attacks can be effectively protected and user data can be ensured.

Methods to optimize PHP session performance include: 1. Delay session start, 2. Use database to store sessions, 3. Compress session data, 4. Manage session life cycle, and 5. Implement session sharing. These strategies can significantly improve the efficiency of applications in high concurrency environments.

Thesession.gc_maxlifetimesettinginPHPdeterminesthelifespanofsessiondata,setinseconds.1)It'sconfiguredinphp.iniorviaini_set().2)Abalanceisneededtoavoidperformanceissuesandunexpectedlogouts.3)PHP'sgarbagecollectionisprobabilistic,influencedbygc_probabi

In PHP, you can use the session_name() function to configure the session name. The specific steps are as follows: 1. Use the session_name() function to set the session name, such as session_name("my_session"). 2. After setting the session name, call session_start() to start the session. Configuring session names can avoid session data conflicts between multiple applications and enhance security, but pay attention to the uniqueness, security, length and setting timing of session names.

The session ID should be regenerated regularly at login, before sensitive operations, and every 30 minutes. 1. Regenerate the session ID when logging in to prevent session fixed attacks. 2. Regenerate before sensitive operations to improve safety. 3. Regular regeneration reduces long-term utilization risks, but the user experience needs to be weighed.

Setting session cookie parameters in PHP can be achieved through the session_set_cookie_params() function. 1) Use this function to set parameters, such as expiration time, path, domain name, security flag, etc.; 2) Call session_start() to make the parameters take effect; 3) Dynamically adjust parameters according to needs, such as user login status; 4) Pay attention to setting secure and httponly flags to improve security.

The main purpose of using sessions in PHP is to maintain the status of the user between different pages. 1) The session is started through the session_start() function, creating a unique session ID and storing it in the user cookie. 2) Session data is saved on the server, allowing data to be passed between different requests, such as login status and shopping cart content.

How to share a session between subdomains? Implemented by setting session cookies for common domain names. 1. Set the domain of the session cookie to .example.com on the server side. 2. Choose the appropriate session storage method, such as memory, database or distributed cache. 3. Pass the session ID through cookies, and the server retrieves and updates the session data based on the ID.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version
SublimeText3 Linux latest version