Home  >  Article  >  Backend Development  >  PHP mathematical function practice three: clever use of random function rand()

PHP mathematical function practice three: clever use of random function rand()

藏色散人
藏色散人Original
2021-07-28 11:31:403424browse

In "PHP Mathematical Function Practice 2: All-round Application of the Round() Function", we introduce to you the role of the PHP mathematical function round (rounding floating point numbers) through specific code examples. ) and how to use it, simple and easy to understand.

In this article, we continue to introduce the use of the commonly used mathematical function rand in PHP. Why is it said to be commonly used? Presumably in the PHP development process, the use of random numbers is relatively common, so the role of the rand function is to generate random integers.

Old tradition, we start with a specific question, "Please write a PHP script to generate a random 11-character alphanumeric string".

Give everyone 3 seconds to think about the solution to this problem

1~

2~

3~

Too simple!

First of all, without further ado, create a PHP sample file demo.php

PHP mathematical function practice three: clever use of random function rand()

##The specific code is as follows:

<?php
$x = rand(10e12, 10e16);
echo base_convert($x, 10, 36)."\n";

The above code The parameter 10e12 represents the minimum number returned, and the parameter 10e16 represents the maximum number returned. Then the generated random integer $x is converted between bases through the base_convert function.

Let’s run it and see if the output is a random 11-character string of letters and numbers?

Output result:

ptkkl0hphh9

Refresh it again and it becomes

m71otm3mmma

OK, the problem is solved!

Function summary:

rand() function syntax "rand();" or "rand(min,max);". If you want a random integer between 10 and 100, inclusive, use rand (10,100).

The base_convert() function syntax is "base_convert(number, frombase,tobase);". The parameters inside respectively represent: the number to be converted, the original base of the specified number and the base to be converted. (Between 2 and 36, including 2 and 36, numbers higher than decimal are represented by letters a-z, for example, a represents 10, b represents 11 and z represents 35)

Note: The mt_rand() function is generated A better choice for random values. The speed of returning results is 4 times that of the rand() function. We will introduce this to you in a follow-up article. Please pay more attention~

There are many videos on the PHP Chinese website platform Teaching resources, everyone is welcome to learn:

https://www.php.cn/course/list/29/type/2.html

The above is the detailed content of PHP mathematical function practice three: clever use of random function rand(). For more information, please follow other related articles on the PHP Chinese website!

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