Erik Østergaard - Random Number Generator (RNG) / Tilfældige tal generator

## Random Number Generator (RNG)

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

 Select Lower Range Number Select Upper Range Number Result Get number now! --- Choose --- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 --- Choose --- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

### Random Number Generator (RNG) - generate one random number at a time

#### General description

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

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 "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 two SELECT tags for input of the user selected Lower Range Number and Upper Range Number. 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...

• Select a Lower Range Number.
• Select an Upper Range Number.
• Select a Lower Range Number less than and not equal to the Upper Range Number.

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.