PHP program to generate a random number in a given range (min, max)

| | | | | | | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

Using the rand() function: The rand() function generates a pseudo-random number between a given range, or between 0 and the default maximum (getgrandmax()), which is system dependent.Syntax:
int rand ($min, $max) 
Parameters:the rand() function takes two optional parameters as above and described below.
  • $min: is an optional parameter used to set the lower limit for the random number. The default min is 0.
  • $max:This is an optional parameter used to set an upper limit for the random number. The default is max - this is the return value of getgrandmax(), which is system dependent (for Windows it is 32767).
Program 1:PHP program to generate a random number using the rand function().
// PHP program for generating a random number
// within the specified range
// Generate random numbers without a range
// the rand() function returns a random number $num1 = rand(); echo " Random number: " . $num1 . "" ;
// Generate random numbers in a given range $num2 = rand (7, 100); echo "Random number in range (7, 100):" , $num2 ;
?>
Exit:
Random number: 77551982 Random number in range (7, 100): 37 
Note.The rand() function - it is a pseudo-random function which means that it takes a seed from the machine and generates a number according to it. Thus, the method for generating numbers is not completely random. This can be tracked to a certain extent. So it is not cryptographically secure. It is not used for cryptography where randomization is very important. Use the random_int() function to generate a cryptographically secure random number. Using the random_int() function: random_int() is used to generate cryptographically secure random numbers. These numbers can be used for objective results. CryptGenRandom() function on Windows and getrandom (2) system call on Linux to generate a random number.Syntax:
int random_int ($min, $max) 
Parameters:The random_int() function takes two parameters as above and described below.
  • $min: contains the lower limit of the random number.
  • $max:contains the upper limit of the random number.
Program 2: Generate random numbers in a range using the random_int() function.
// PHP program for generating a random number
// in a given range
// Generate random numbers in a given range
// using the random_int() function $num1 = random_int (35 , 100); echo " Random number in range (35, 100): " . $num1 . "" ;
// Random number in the range (10, 30) $num2 = random_int (10, 30); echo " Random number in range (10, 30): " . $num2 ;
?>
Exit:
Random number in range (35, 100): 93 Random number in range (10, 30): 28 

👻 Read also: what is the best laptop for engineering students?

We hope this article has helped you to resolve the problem. Apart from PHP program to generate a random number in a given range (min, max), check other code Python module-related topics.

Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Julia Nickolson

Tallinn | 2023-03-22

Thanks for explaining! I was stuck with PHP program to generate a random number in a given range (min, max) for some hours, finally got it done 🤗. I just hope that will not emerge anymore

Chen Gonzalez

Moscow | 2023-03-22

Simply put and clear. Thank you for sharing. PHP program to generate a random number in a given range (min, max) and other issues with code Python module was always my weak point 😁. I just hope that will not emerge anymore

Anna Zelotti

London | 2023-03-22

StackOverflow is always a bit confusing 😭 PHP program to generate a random number in a given range (min, max) is not the only problem I encountered. Will use it in my bachelor thesis

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically