How to create or generate random number in php?
creating or generating random number using javascript?
Explanation
Follow the steps to create Random Numbers
Getting Random Numbers in PHP:
Its very easy to get random numbers in php too.
PHP gives a method called "rand()".
Using the method we can get random numbers.
Calling rand() will return values between 0 and RAND_MAX
e.g:
<?php
$numb = rand(); //Here we create a random number
echo($numb);
?>
Result:
270478616
So now we have to get values between x to y say 50 to 66.
It can be achieved by following the example
e.g:
<?php
$numb = rand(50,66); //Here we create a random number between 50 and 66
echo($numb);
?>
Result:
54
Getting Random Numbers in javascript:
Its very easy to get random numbers in javascript.
javascript gives a method called "random()".
Using the method we can get random numbers.
Calling Math.random() will return values between 0 and 1
e.g:
<script>
var numb = random(); //Here we create a random number
document.write(numb);
</script>
Result:
So now we have to get values between x to y say 0 to 66.
It can be achieved by following the example
e.g:
<script>
var numb = Math.random(); //Here we create a random number e.g: .23xxxx
numb = numb*66; //Here we multiply the number by 66. e.g: the value will convert to 15.xxxx
numb = Math.round(numb); //Now we round of the value. e.g: the value will be rounded to 15
document.write(numb); //So we have got a random number inbetween 0 and 66
</script>
Result:
A Note
Learn PHP programming language tutorial with simple and neat example. Hope you enjoy this free tutorial.
Do give us your valuable feedback and suggestions on this online tutorial. This is a Copyright Content.