Jump to content

Random number

From Wikiversity

A number selected by chance (or in the case of a computer program, as near to simulating chance as possible).

Typically, a programming language has a built in command or function to generate a number for you. In most cases, the number will be delivered as a random floating point decimal value between 0 and 1. It is up to the programmer to apply arithmetic to this number to convert it to a number in the desired range.

For example, if you want to simulate the roll of a six-sided die, you would multiply this original random result (which starts out between zero and one) by six, add one and then remove any portion after the decimal.

  Let DieRoll = Integer(RandomResult * 6 + 1)


In the most extreme cases, where RandomResult is 0 (multiplying by six yields zero, and then add one) our die roll is a one, and when RandomResult is 0.999999... (multiplying by six yeilds 5.9999..., adding one yeields 6.9999..., but taking only Integer portion) we get six.