How to set up a randomizing function
Overview
Generating a random entity on a page can be split into three sections:
2) defining a randomizing function
3) using that function to return your desired entity
Defining your entities
In this case, we wish to generate a random quote, so we need to define a long list of quotes from which a script can pick one out. To do this, we define an array. An array is basically a one-dimensional matrix that contains every possible piece of information in it you would like displayed. It is from the array that the randomizing function will pick the random (in this case) quote.
array: quotes = new Array
(
"This is quote 1",
"This is quote 2",
"This is quote 3",
...
"This is the last quote"
); array is named "quotes" and that the last quote does not have a comma before the close parenthesis. If it did have a comma, the script wouldn’t work. Defining a randomizing 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);
};
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. Displaying the random entity
Once we have a randomizing function, returning a random entity is straightforward: we simply call upon the script to write the quote associated with the number generated by the random function:
document.write(quotes[rand(quotes.length)-1]) rand is the function as defined before, (quotes.length)-1 is the range of quotes we defined in the array and document.write is the JavaScript command that returns the "value" created within the parenthesis. It is then this entity that will be displayed on the webpage. quotes = new Array
(
"This is quote 1",
"This is quote 2",
"This is quote 3",
...
"This is the last quote"
);
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);
};
document.write(quotes[rand(quotes.length)-1])
script tag within the html header or body: <script type="text/javascript">
insert script here
</script> <script type="text/javascript" src="script.js">
</script> Further reading
Apart from a little bit of advice from a couple of friends, the majority of the JavaScript employed on this website has its roots in w3schools.com, the best starting point to learn about anything web-related on the web. It is strongly recommended you read through their tutorials on JavaScript (and indeed html) before getting started on anything, mainly because they actually know what they are talking about.
If you found this post useful you may also want to check these out:
