Introduction
What can randomization be used for in Javascript/jQuery?
Background
The jQuery library is a Javascript library designed to simplify client-side web page development. With the jQuery library and jQuery syntax, the development effort of interactive web pages is greatly reduced. Javascript provides Math.floor and Math.random methods that can be used together to return random numbers. Random number generation can be useful in a few different situations including the creation of a random quote generator or a random image loader.
Demo of random number generator here.
Demo of random quote generator here.
Demo of random image loader here.
Using the code
Using the following JavaScript line to return a random number:
Collapse | Copy Code
Math.floor((Math.random() * 100000000) + 1);
The result is a random number between 1 and 100000000, though not as random as it could be. To further randomize, you can use power law distribution. Random number generator using the following line code:
Collapse | Copy Code
//
Math.round(Math.exp(Math.random()*Math.log(10000000-0+1)))+0;
//
The result is a random number between 1 and 100000000, with a random number of digits.
To build the random quote generator, we compile a long list of quotes. A list of quotes can be found here. Viewing the source, you will find each quote is separated by 2 BR tags. Put your own quote.html page on your server.
Embed the ShowRandomQuote function on your page and call with an onclick event.
The page must have a DIV tag with an ID of RandomQuoteDIV.
The page must include a jquery reference.
Collapse | Copy Code
//
function ShowRandomQuote() {
$.get('quote.html', function(data) {
var quotes = data.split("<BR><BR>");
var idx = Math.floor(quotes.length * Math.random());
var zhtml = "<table><TR><TD>";
zhtml += quotes[idx] + "</td></tr></table>";
$('#RandomQuoteDIV').html(zhtml);
$('#RandomQuoteDIV').show();
});
}
//
To build the random image loader, we use a page with many, many images. My image page with over 1000 images can be found here. Put your own image page (index.html) on your server.
Put the ShowRandomImage function on your page and call with an onclick event.
The page must have a DIV tag with an ID of RandomImageDIV.
The page must include a jquery reference.
Collapse | Copy Code
//
function ShowRandomImage() {
$.get('../all/index.html', function(data) {
var idx = 0;
$('#RandomImageDIV').html(data);
var imageArray = [];
$("img").each(function(){
imageArray.push($(this).attr("src"));
idx++;
});
var idxRAND = Math.floor(imageArray.length * Math.random());
var zahtmlstring = "<table><TR><TD><img src='" + imageArray[idxRAND] + "'>";
zahtmlstring += "</td></tr></table>";
$('#RandomImageDIV').html(zahtmlstring);
$('#RandomImageDIV').show();
});
}
//
Questions, issues, concerns? View the working demo and source code at the following links:
Demo of random number generator here.
Demo of random quote generator here.
Demo of random image loader here.
Points of Interest
Randomization is quick and easy in Javascript and can be applied to a wide variety of uses in javascript including random number generators, randomly displaying a quote, and randomly loading an image.
Source from
http://www.codeproject.com/Tips/834201/Javascript-jQuery-Random-Number-Generator-Random-Q
No comments:
Post a Comment