Ticker

6/recent/ticker-posts

Random Method and Random password generation using Random method


 Computer programs that solve real world problems are much larger.To maintain a large program is to construct it from small,simple pieces or modules.Technique is divide and conquer.Module in java-script is called function.

In javascript module that is already built module is called method.That method is accessed using object.Different object in javascript(Math,String,array etc) and functions according to the object.Built in function in javascript is called method.Example random() function is built in function of object Math.

Function that is created by user according to the need of user.That function is set of lines that perform specific task.That is called function in javascript. Example

function Square(var x) 

{

 var result=x*x;

return(result);

}

That is user defined function.That square a number x and store result in variable result.

The prepacked functions that belong to JavaScript objects(Math.Pow,Math.round) are called methods.Function define specific task that may be used at many points in a script.These functions are programmer-defined functions.

Function is invoked by a function call.The function call specifies the function name and provides information(as arguments).That the called function needs to perform its task.

Format of a function is

function function-name(parameter)

{

declarations and statement;

}

Function name is valid identifier.The parameter list is a comma-separated list containing the name of the parameters received by the function when it is called.There should any value be one argument in the function call for each parameter in the function. If a function does not receive any values.The parameter list is empty.The declaration and statement in braces  from the function body.

If a function does not return a result,control returns when the program reaches the function ending right braces or by executing the statement.

return;

If function does return a result.

return expression;

Return the value of expression to the caller.When a return statement is executed,control returns immediately to the point at which the function was invoked.All variable declared in function definitions are local variables.This means that they can be accessed only in the function in which they are defined.Functions have a list of parameters that provide the means for communicating information between function and function calls.A function parameter also considered to be local variables.We did not have to define how to convert string to integer java-script already provide function parseInt.

Reasons for modularizing a program with functions

  • Divide and conquer approach makes program development more manageable.
  • Software re-usability(Using existing functions as building blocks to create new programs).
  • Avoid repeating code in a program code that is packaged as a function can be executed from several locations in a program by calling the function.
  • Minimize the time taken for development.This enables developers to work simultaneously and minimize time.
  • Separate modules are easier to test,implement and design .
  • Modular programming programmers can collaborate and work on the same application.Small team focus on individual parts of the same code. 

Random Number Generation

The high rollers at the Plush mahogany and felt craps table to the quarter proppers at the one armed bandits.It is element of chance. The possibility that luck will convert a pocketful of money into a mountain of wealth.The element of chance can be introduced through the Math object random method(call random a method because it belongs to the Math object).

var randomvalue=Math.random();

Method random generates a floating point value 0.0 up to but not including 1.0.The range of values produced directly by random is often different than what is needed in a specific application.

Example rolling a six sided die would require random integers in the range of 1 to 6.

Math.floor(1+Math.random()*6)

Multiplies the result of a call to Math random() by 6 to produce a number in the range 0.0 upto but not including 6.0.This is called scaling the range of the random numbers.Add 1 to the  result to shift the range of numbers to produce a number in the range 1.0 up to but not including 7.0.

For example if random method generate 0.5

so Math.floor(1+0.5*6)

Math.floor(1+3)

so random number generate by random function is 4.

Math.floor to round the result down to the closest integer not greater than the arguments value.

Code to generate random password using random method using html and JavaScript.

<html>

<head>

<script>

function r()

{

var c="!@#$%^&*()_+qwertyuiopasdfghjklzxcvbnm<>";

var p="";

for(var i=1;i<=6;i++)

{

var k=Math.floor(Math.random()*c.length)+1);

p=+c.charAt(k);

}

document.getElementById("n").value=p;

}

</script>

</head>

<body>

password<input type="text" size="15" id="n"><br>

<button type="button" onclick="r()">Random password generate</button>

</body>

</html>

Output of above code is random password of 6 length.Random image generation also use random function that generate image randomly.

Here <script> tag is used to add javascript code. function r() is used to generate password. That r() function generate 6 length password using random method.When button is pressed r() function is called  and password store in text box.











Post a Comment

0 Comments