DK-Flag Erik Østergaard - Random Number Generator (RNG) / Tilfældige tal generator Go to Home Page
 Dice Return
Bottom of This Page

Random Number Generator (RNG)

- generate several random numbers at a time

Make you choices below and... Click 'Calculate':

Select Select Select Select
Lower Range Number Upper Range Number Total random number(s)    Generated numbers only come up once  
   Sort result       Ascending [A-Z]
    Descending [Z-A]
Result Sum total   Get number(s) now!

Random Number Generator (RNG) - generate several random numbers at a time

General description

Use "The Random Number Generator" (RNG) for example as 1 die by setting the range from 1 to 6 and the total number to 1 or as 2 dice by setting the range from 1 to 6 and the total number to 2 etc. Or use it in a "numbers game" to generate random numbers in a certain range. Maximum range is from 1 to 40 with a total number from 1 to 10 in this version.

This script gives the user the possibility of generating up to ten random numbers at a time (by every push of the button) and to choose that the randomly generated numbers only come up once. It is even possible to have the random numbers sorted and to choose the sort order.

Dice (plural) or singular die: Small cubes of plastic, ivory, bone, or wood, marked on each side with one to six spots, usually used in pairs in games of chance or in gambling, played by shaking and throwing from one to six dice onto a flat surface, which generates a random result by chance.

Random: 1. Proceeding, made, or occurring without definite aim, purpose, or reason: the random selection of numbers. 2. Statistics. Of or characterizing a process of selection in which each item of a set has an equal probability of being chosen.

Chance: 1. The absence of any cause or series of causes of events as they actually happen that can be predicted, understood, or controlled. 2. Luck or fortune: a game of chance.

Technical description

This is a demonstration "Random Number Generator" (RNG) script implemented in JavaScript. You must have a JavaScript-capable browser for this to work. Note that keyboard entry is disabled in this script. The code can also dynamically change the selections made.

Because I want this "Random Number Generator" (RNG) script to be interactive, both receiving and processing input, I embed the "Random Number Generator"-table within FORM tags. The buttons are specified as input sources. The FORM tag lacks a METHOD attribute. Normally this attribute would be required, specifying either POST or GET, depending on how input is to be passed to an application through CGI. The FORM tag here also lacks an ACTION attribute, which would normally tell the browser the location of the CGI program for processing the form. Since my form will be processed on the client side, neither of these attributes is required. Instead my FORM simply has a NAME attribute, enabling the script to distinguish forms by name later on. The INPUT tags within this form also differ from the usual tags. TYPE is specified simply as "button", which is not one of the normal options. Also, the INPUT tag contains a new attribute, onClick. This pair, button and onClick, specifies one means of passing input to the script. For the "Random Number Generator" script the two INPUT buttons are the only input source used with the exception of the three SELECT tags for input of the user selected Lower Range Number, Upper Range Number, and Total random number(s). Furthermore two INPUT tags with TYPE specified as "checkbox" are added for the user to select "Generated numbers only come up once" and "Sort result". Last-mentioned with the two INPUT tags with TYPE specified as "radio" to match are added for the user to select "Ascending [A-Z]" (default) or "Descending [Z-A]" sort order. Because browsers ignore HTML tags and attributes they do not understand, these new elements will not cause particular problems with the appearance of the "Random Number Generator".

When this script is being run it also validates the user's input or in this case more precise the user's selections or choices being made and if required responds with an appropriate error message such as these in checking order...

Note that no such thing as a software-generated "random" number really exists.

Even the best algorithms, such as the one presented in the next paragraph, don't generate truly random numbers. Instead, they generate very long sequences of numbers that simulate random behaviour. However, eventually the sequences repeat. The numbers generated are therefore properly known as pseudo-random numbers.

The RandomNumberGenerator (RNG) with the function "NextRandomNumber()" and the function "RandomNumberGenerator()" is an implementation of the Park-Miller algorithm. (See "Random Number Generators: Good Ones Are Hard to Find", by Stephen K. Park and Keith W. Miller, Communications of the ACM, 31(10):1192-1201, 1988.) The JScript version was written by David N. Smith of IBM's T. J. Watson Research Center. Mr. Smith notes that his version has not been subjected to the rigorous testing required of a mission-critical RNG.

You might have noticed that JScript's Math object includes a built-in random() method. The version presented here should work as well as, if not better than, the built-in implementations, and will work uniformly on all platforms.

In the JavaScript code Listing, the "RandomNumberGenerator()" constructor uses the system time, in minutes and seconds, to "seed" itself, that is, to create the initial values from which it will generate a sequence of numbers.

This RNG is implemented as an object. To use it, you create an instance of the object and then invoke its next() method to return a number. Each call to the next() method returns a new random number.

Like many random number generators, this RNG returns a fraction between 0 and 1; for example, 0.2755983265971, or something similar. To convert this number to an integer between 0 and n, you must multiply n times the random number and then round the result.

Before the actual sort is performed for the sort option used in the JavaScript code it is necessary first to split the delimited or separated text string with the result into substrings and create an array containing the resulting text substrings with the separators or delimiters removed. In the JavaScript code Listing, the utility function "doSplitString()", which splits a string (compatible with JavaScript 1.0) into an array of strings by separating the string into substrings, is used for this purpose together with the array initialization function "MakeArray()".

To the sort algorithm for the sort option used in the JavaScript code has the Bubble Sort been chosen. The array-oriented sort routine bubble sort greatest virtue is its simplicity. It is very fast on nearly sorted items, but its general performance is relatively poor. The bubble sort algorithm is very simple: successive sweeps are made through the items to be sorted, and on each sweep the largest item is moved closer to the top, rising slowly like a bubble - hence the name. Because each sweep places one item into its final and correct position, the next sweep need not re-examine this item. For the sorting of the items is used an array of n items. An analysis of the order of this algorithm is straightforward: it makes n passes through the items, and on each pass it performs n-1 comparisons and possibly this number of swaps. Thus, the algorithm's running time will be proportional to n(n-1), or more simply just n². The code takes advantage of the fact that if no swaps are made on any given pass, the items are sorted and the sort can terminate. In the JavaScript code Listing, the utility function "doBubbleSortArray()" performs the sort.

Note that if an array's items (elements) are in text string variables and all consist of integer numbers or floating-point numbers you can with advantage use the built-in function "parseInt()" or "parseFloat()" to parse the string arguments to the appropriate number type variables in the array before a sort is performed - otherwise the sort order of the numbers will not be correct.

You can see the JavaScript by using View Source.

See also...

   Top of This Page
   Go to Home Page