PHP Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
PHP Topics
Introduction Introduction
Syntax Syntax
Data Types Data Types
Operators Operators
Control Structures Control Structures
Functions Functions
Pre-defined Function Pre-defined Function
Calendar Functions Calendar Functions
Date and Time Date and Time
Array Functions Array Functions
Array List Array Functions List1
Array Function List Array Functions List2
Math Functions Math Functions
PHP MYSQL Functions PHP Mysql Functions
File Handling File Handling
Error Handling Error Handling
DB Size DB Size
PHP Mail PHP Mail
String Tokens String Tokens
String Functions String Functions
String Functions List String Functions List1
String Functions List2 String Functions List2
Session Functions Session Functions
Cookies Functions Cookies Functions
Form Variables Form Variables
Running PHP from JS Running PHP from JS
Array To JS Array To JS
JS Array Array to PHP
Encryption Encryption
Common Header Common Header
Forums Ask Your Doubts
Scraps More about PHP
Feedback Feedback
 




PHP rand() - Math Functions


Tutorials »Php »

Topic

What is rand Function?
How to create or generate random number in php?
creating or generating random number using javascript?



Explanation

The "rand()" function is used to generate a random integer.

Syntax:
    rand(min,max)
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: 248810346

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: 50

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.

Other Links

web hosting