You can vote for 25 pieces of data. you can only vote for 16 pieces of data at a time. The previous programmer threw a loophole and forgot to put the voting into the database. the voting sequence generated by 200 users was empty. So how do you fill in this leak? Of course, reflect the situation to the superior. However, we will discuss the technology here, that is, we need to generate 16 non-repeated random numbers between 1 and 25 to fill. How to design functions? Store random numbers into arrays
You can vote for 25 pieces of data. you can only vote for 16 pieces of data at a time. The previous programmer threw a loophole and forgot to put the voting into the database. the voting sequence generated by 200 users was empty. So how do you fill in this leak?
Of course, reflect the situation to the superior. However, we will discuss the technology here, that is, we need to generate 16 non-repeated random numbers between 1 and 25 to fill. How to design functions? Store a random number into an array, and then remove duplicate values in the array to generate a certain number of non-repeated random numbers.
The program code is as follows:
The program runs as follows:
2,3,4,6,7,8,9,10,11,12,13,16,20,21,22,24
Additional notes:
- Used to generate a random number? Mt_rand ()? Function. What is the average speed for this function to generate random numbers? Rand ()? Four times faster.
- The "flip method" is used to remove duplicate values in the array? Array_flip ()? Exchange the key and value of the array twice. Is this practice more useful? Array_unique ()? Faster.
- Before returning an array, use? Shuffle ()? Assign a new key name to the array to ensure that the key name is a continuous number ranging from 0 to n. If you do not perform this step, the key name may not be consecutive when you delete duplicate values, which may cause trouble for traversal.
Note: this article