Hongmu Notes
Home Language Notes PHP generates random numbers with specified number of digits
Language Notes PHP

PHP generates random numbers with specified number of digits

PHP generates random numbers with specified number of digits

What should I do if I want to generate a random number of seventy or eighty digits?

To solve this problem, I wrote a function.


function rand_num($num = 1){
    $num = (int) $num;
    $rand = '';
    if($num > 1){
        for($i=0;$i<$num;$i++){
            $rand = $rand . mt_rand(1,9);
        }
        return $rand;
    }else{
        return mt_rand(0,9);
    }
}

What about this function. It’s also very simple, you can get it if you need it.

Calling the method is naturally rand_num (the number of digits you want)

 

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP ob function record

PHP ob function record Language Notes PHP

Usage of the following three functions ob_get_contents(); ob_end_clean(); ob_start(); You can use these functions to buffer local files and execute local script code. Use ob_start() to save the output code into the buffer, and the page will not be displayed; then use ob_get_contents to get the data in the buffer. o…
👁 141
PHP preg

PHP preg Language Notes PHP

The preg_match_all function is used to perform a global regular expression match. preg_match_all() Syntax int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG…
👁 169
Detailed explanation of PHP ternary operator and if

Detailed explanation of PHP ternary operator and if Language Notes PHP

Ternary operator condition ? Result 1 : Result 2 Explanation: The position in front of the question mark is the condition for judgment. If the condition is met, the result is 1, and if it is not met, the result is 2. This article compares and explains the ternary operator and if...else... in detail. I hope it will be helpful to everyone. Today when I was revising my paper online, I encountered a statement that I couldn’t understand: $if_summary = $row['IF_SUMMARY']=…
👁 190
PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232

Recommended reading

Responsive Hair Styling Design Website Template 0455

Responsive Hair Styling Design Website Template 0455 Practical Collection Yiyou template

An eyouCMS responsive website template designed for the hair styling and design industry. Its fashionable and sophisticated design seamlessly showcases hair styling portfolios, design services, brand stories, and current industry trends. This template helps hair styling businesses attract online customers and enhance their brand awareness. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for YououCMS...
👁 37
Responsive Smart Bathroom Product Website Template 0456

Responsive Smart Bathroom Product Website Template 0456 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the smart bathroom products industry, featuring a modern, tech-driven design style that effectively showcases bathroom products, smart systems, spatial designs, and brand identity. It enables bathroom brands to present their products online and attract customers in the home decoration and construction sectors. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Questions for EyouCMS | EyouCMS...
👁 42
Responsive specialty food and coarse grain snack website template – 0457

Responsive specialty food and coarse grain snack website template – 0457 Practical Collection Yiyou template

An eYouCMS responsive website template designed for specialty foods, whole grains, and snacks. Its appealing design effectively showcases specialty food products, whole grain items, snack brands, and sales information, helping food brands present their products online and attract consumers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for eYouCMS | eYouCMS...
👁 56
Resource website typecho001 template

Resource website typecho001 template Program Notes Typecho

typecho001 template template please do not modify the folder name of this template. The folder name is: typecho001 1.4 Fix the js output problem on the article page 1.3 Fix the error prompt when the plug-in is not installed Optimize the list page code output 1.2 Fix the comment function and add a custom homepage title 1.1 Fix the comment reply asymmetry function Add a separate title setting function on the homepage Add the website favicon.ico icon Add one...
👁 198
Responsive branded sportswear website template 0458

Responsive branded sportswear website template 0458 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the branded sportswear industry; its vibrant and fashionable design makes it perfect for showcasing sportswear products, brand identity, athletic equipment, and new product launches. It helps sportswear brands showcase their products online and attract sports enthusiasts. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 59
Modification of typecho paging style

Modification of typecho paging style Program Notes Typecho

Sir, times have changed! Typecho is currently the most perfect solution, because Baidu can only see the code of fixed thinking. The actual generated HTML code is breathtakingly clean and fully customized, including adding classes to the li element, adding classes to the a element, adding classes to the previous page and next page, and removing the li tags that come with typecho to express more. I can even add some text to the content inside...
👁 517