phptutorials

PHP Program to print Random Numbers

In this post, we are going to see how to print Random numbers in PHP. It is very simple in PHP because it has an in-build function called rand($min, $max) that does the job. Let me show you a simple example first.

First example program we are going to see is “How to print random numbers between 1 and 100 in PHP”:

As I said before, we have an in-build function called rand($mix, $max) which accepts two parameters, min and max value. So just giving the min & max values in the first & second parameters does the job. And for every refresh, it is going to give a random value between these two.

Now, let’s write another example for the same rand($mix, $max) function where you can give the min and max value dynamically.

In the above example, we are using an HTML form to get the min & max values from the user, then using PHP we are capturing the values and passing it to function rand($mix, $max) to generate a random number.

I hope this gives a clear picture of how to generate a Random number between two values in PHP.

You can actually see the demo for the same in here, just click the below button to see it.

demo

Related Posts

Leave a Reply