Click here to Skip to main content
15,887,822 members
Articles / Programming Languages / Javascript
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.58/5 (8 votes)
5 Feb 2021CPOL2 min read 39K   11   1
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:

C++
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:

C++
//
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.

C++
//
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.

C++
//
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)


Written By
Oproot Research
United States United States
+ Oproot Research
Learn all that is learnable.

Comments and Discussions

 
GeneralMy Vote 5 Pin
Shemeemsha (ഷെമീംഷ)28-Oct-14 23:42
Shemeemsha (ഷെമീംഷ)28-Oct-14 23:42 
Nice one Thumbs Up | :thumbsup: Thumbs Up | :thumbsup: Thumbs Up | :thumbsup:

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.