Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Javascript

Javascript, jQuery: Random Number Generator, Random Quote Generator, Random Image Loader

4.62/5 (9 votes)
28 Sep 2024CPOL2 min read 45.3K  
JavaScript provides Math.floor and Math.random methods that can be used together to return random numbers.
Random number generation can be useful in different situations including the creation of a random quote generator or the creation of a random image loader.

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 loader here

Using the Code

Using the following JavaScript line to return a random number:

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

JavaScript
//
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.

JavaScript
//
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.

JavaScript
//
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();
});
}
//

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.

History

  • 12th November, 2014: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)