Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

How to Setup a Randomising Function

By Arbitrary Constant
2007-11-17


Defining a randomising function

Once you have an array from which to pick a random entity, there needs to be a function that will deliver a random result to pick said entity. As you would expect, there are many ways to do this and the following method is one example. In it, the script utilises the time and date to pick a random entity from the array. It is given by the following:
rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd()
{
rnd.seed = (rnd.seed*9301+49297) % 233280;
return rnd.seed/(233280.0);
};

function rand(number)
{
return Math.ceil(rnd()*number);
};
The first two lines define two elements, based on the date and time, that are to be used in the next line, which is the function rnd(). This function is defined basically as a little sum which uses the time and date as taken before which in turn is then used in the function rand(number) to define a function that will return a random number based on the time and date.


Tutorial Pages:
» How to set up a randomising function
» Defining your entities
» Defining a randomising function
» Displaying the random entity


 | Bookmark
Related Tutorials:
» JavaScript Debugging Techniques with Firebug
» Striped Tables Using JavaScript
» Opening PDFs in a New Window with JavaScript
» Essential Javascript -- A Javascript Tutorial
» Submit Forms Conditionally using JavaScript
» Introduction to JavaScript Tutorial