DK-Flag Erik Østergaard - Dice / Terning Go to Home Page
 Dice Return
Bottom of This Page

Dice Roll Counter:
Roll of dice:

Roll of Dice

General description

Use "The Roll of Dice" as 1 die.

This script gives the user the possibility of generating one random number at a time (by every push of the button).

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 "Roll of Dice" script implemented in JavaScript. You must have a JavaScript-capable browser for this to work. Note that keyboard entry is disabled in this script.

Because we want this "Roll of Dice" script to be interactive, both receiving and processing input, we embed the "Roll of Dice"-table within FORM tags. The button is specified as an input source. 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 our form will be processed on the client side, neither of these attributes is required. Instead our 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 "Roll of Dice" script this is the only input source used. Because browsers ignore HTML tags and attributes they do not understand, these new elements will not cause particular problems with the appearance of the "Roll of Dice".

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.

You can see the JavaScript by using View Source.

See also...

   Top of This Page
   Go to Home Page