Click here to Skip to main content
13,149,756 members (82,085 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

20.3K views
10 bookmarked
Posted 12 Nov 2014

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

, 12 Nov 2014
Rate this:
Please Sign up or sign in to vote.
Javascript provides Math.floor and Math.random methods that can be used together to return random numbers.

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 / quote / image loader here.

Using the code

Using the following JavaScript line to return a random number:

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:

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

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

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

No changes.

License

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

Share

About the Author

The Operator
Oproot Research
United States United States
+


Oproot Research
Research, Development
Science Experiments
Photography, Reading

You may also be interested in...

Comments and Discussions

 
GeneralMy Vote 5 Pin
Shemeemsha (ഷെമീംഷ )28-Oct-14 23:42
memberShemeemsha (ഷെമീംഷ )28-Oct-14 23:42 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170924.2 | Last Updated 12 Nov 2014
Article Copyright 2014 by The Operator
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid