H I O X INDIA
Online PHP Tutorial
 HOME  ||  Scripts  ||  Purchase  ||  Tutorials  ||  Images  ||  Tools  ||  Templates 
  :-)  Send Page   :-)   Feedback   :-)   Register   :-)   Links   :-)   Support   :-)
Español Français 中文 Deutsch Portuguese Japanese தமிழ்
 Forums   Hosting   Internet Stats   Easy Calculation   FUN Games 

PHP Topics
Introduction
Syntax
Data Types
Variables
Operators
Control Structures
Functions
Pre-defined Function
Calendar Functions
Date and Time
Array Functions
Array Functions List1
Array Functions List2
Math Functions
PHP Mysql Functions
File Handling
Error Handling
DB Size
PHP Mail
String Tokens
String Functions
String Functions List1
String Functions List2
Session Functions
Cookies Functions
Form Variables
Running PHP from JS
Array To JS
Array to PHP
Encryption
Common Header
Ask Your Doubts
More about PHP
Feedback





Random Number in PHP and Javascript


Topic

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

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

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:




privacypolicy     licence     sitemap
© 2004-2010 HIOX INDIA