notes_20on_20the_20use_20of_20rnd
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
notes_20on_20the_20use_20of_20rnd [2018/03/31 13:19] – external edit 127.0.0.1 | notes_20on_20the_20use_20of_20rnd [2024/01/05 00:21] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
=====Notes on the use of RND===== | =====Notes on the use of RND===== | ||
- | //by Richard Russell, February 2010, revised May 2013//\\ \\ | + | //by Richard Russell, February 2010, revised May 2013// |
+ | |||
+ | This article expands on the description of [[http:// | ||
===== Variants of RND ===== | ===== Variants of RND ===== | ||
- | \\ | + | |
+ | The **RND** function comes in five variants, as follows: | ||
==== RND ==== | ==== RND ==== | ||
- | \\ **RND** on its own returns a signed 32-bit pseudo-random integer value, i.e. a value in the range -2147483648 to +2147483647. The sequence length is 2^33-1 (8589934591), | + | |
+ | **RND** on its own returns a signed 32-bit pseudo-random integer value, i.e. a value in the range -2147483648 to +2147483647. The sequence length is 2^33-1 (8589934591), | ||
+ | |||
+ | If you call RND precisely 8589934591 times, each of the non-zero integer values (& | ||
==== RND(n) ==== | ==== RND(n) ==== | ||
- | \\ **RND(n)**, where n is a positive integer greater than 1, returns a pseudo-random integer value in the range 1 to n. RND(n) is equivalent to the following function:\\ | + | |
+ | **RND(n)**, where n is a positive integer greater than 1, returns a pseudo-random integer value in the range 1 to n. RND(n) is equivalent to the following function: | ||
+ | |||
+ | <code bb4w> | ||
DEF FNrnd(n%) | DEF FNrnd(n%) | ||
LOCAL R | LOCAL R | ||
Line 13: | Line 25: | ||
IF R < 0 R += 2^32 | IF R < 0 R += 2^32 | ||
= R - n% * INT(R / n%) + 1 | = R - n% * INT(R / n%) + 1 | ||
- | Note that the statistical distribution of the returned values depends on the value of **n**. For small values of **n** the probabilities of each of the possible returned values (1 to n) are very nearly equal. For example if n=7 the likelihoods of the different values are as follows:\\ \\ 1: 1227133513 / 8589934591 = 0.142857142857\\ 2: 1227133514 / 8589934591 = 0.142857142974\\ 3: 1227133514 / 8589934591 = 0.142857142974\\ 4: 1227133514 / 8589934591 = 0.142857142974\\ 5: 1227133512 / 8589934591 = 0.142857142741\\ 6: 1227133512 / 8589934591 = 0.142857142741\\ 7: 1227133512 / 8589934591 = 0.142857142741\\ \\ | + | </ |
+ | |||
+ | Note that the statistical distribution of the returned values depends on the value of **n**. For small values of **n** the probabilities of each of the possible returned values (1 to n) are very nearly equal. For example if n=7 the likelihoods of the different values are as follows: | ||
+ | |||
+ | 1: 1227133513 / 8589934591 = 0.142857142857 | ||
+ | 2: 1227133514 / 8589934591 = 0.142857142974 | ||
+ | 3: 1227133514 / 8589934591 = 0.142857142974 | ||
+ | 4: 1227133514 / 8589934591 = 0.142857142974 | ||
+ | 5: 1227133512 / 8589934591 = 0.142857142741 | ||
+ | 6: 1227133512 / 8589934591 = 0.142857142741 | ||
+ | 7: 1227133512 / 8589934591 = 0.142857142741 | ||
+ | |||
+ | So returned values **5**, **6** and **7** are very slightly less probable than values **2**, **3** and **4**, with returned value **1** between these two probabilities. Although this variation is unlikely to be significant, | ||
==== RND(1) ==== | ==== RND(1) ==== | ||
- | \\ **RND(1)** returns a floating-point value in the range 0 <= R < 1, that is the returned value is **greater than or equal to** zero but **less than** one. RND(1) is equivalent to the following function:\\ \\ | + | |
+ | **RND(1)** returns a floating-point value in the range 0 <= R < 1, that is the returned value is **greater than or equal to** zero but **less than** one. RND(1) is equivalent to the following function: | ||
+ | |||
+ | <code bb4w> | ||
DEF FNrnd1 | DEF FNrnd1 | ||
LOCAL R | LOCAL R | ||
Line 22: | Line 50: | ||
IF R < 0 R += 2^32 | IF R < 0 R += 2^32 | ||
= R/2^32 | = R/2^32 | ||
- | Note that the distribution of returned values reflects the distribution of the values returned by **RND**, so the likelihood of the value zero being returned is less than other values. Irrespective of the ***FLOAT** mode in use, RND(1) returns a 40-bit floating point number (32-bit mantissa).\\ \\ | + | </ |
+ | |||
+ | Note that the distribution of returned values reflects the distribution of the values returned by **RND**, so the likelihood of the value zero being returned is less than other values. Irrespective of the ***FLOAT** mode in use, RND(1) returns a 40-bit floating point number (32-bit mantissa). | ||
==== RND(0) ==== | ==== RND(0) ==== | ||
- | \\ **RND(0)** returns the previous random number in RND(1) format. In other words, the pseudo-random-number generator does not advance to the next number in the sequence, but repeats the previous result.\\ \\ | + | |
+ | **RND(0)** returns the previous random number in RND(1) format. In other words, the pseudo-random-number generator does not advance to the next number in the sequence, but repeats the previous result. | ||
==== RND(-n) ==== | ==== RND(-n) ==== | ||
- | \\ | + | |
+ | Setting the parameter of **RND** to a negative integer (i.e. a value in the range -2147483648 to -1) **seeds** the generator (see below) and returns that same value. | ||
===== Seeding the random-number generator ===== | ===== Seeding the random-number generator ===== | ||
- | \\ | + | |
- | DIM pc{l%,h%} | + | As mentioned above, the sequence-length of BBC BASIC' |
- | SYS " | + | |
- | seed% = RND(-ABS(pc.l%)) | + | When //BBC BASIC for Windows// or //BBC BASIC for SDL 2.0// is executed the random number generator is seeded from the CPU Performance Counter: a 64-bit integer value which counts at a rate up to the clock speed of the CPU. |
- | It should be noted, however, that the rate at which the Performance Counter increments is extremely variable between systems, and indeed one isn't guaranteed to exist at all. Therefore if you are lucky enough to have a better source of seed available you should use that instead.\\ \\ | + | It should be noted, however, that the rate at which the Performance Counter increments is extremely variable between systems, and indeed one isn't guaranteed to exist at all. Therefore if you are lucky enough to have a better source of seed available you should use that instead. |
+ | |||
+ | However good your source of seed, since RND(-n) can only take a maximum of 2^31 different values (compared with the total sequence length of 2^33-1) only about **one quarter** of the overall sequence is likely to be exploited. If this is important to you, the pseudo-random number generator can alternatively be seeded by writing values directly to its memory locations, see [[/ | ||
+ | |||
+ | <code bb4w> | ||
!412 = seedl% | !412 = seedl% | ||
?416 = seedh% | ?416 = seedh% | ||
- | Here **seedl%** is a 32-bit value (& | + | </ |
+ | |||
+ | Here **seedl%** is a 32-bit value (& | ||
===== Is RND good enough? ===== | ===== Is RND good enough? ===== | ||
- | \\ | + | |
+ | The built-in RND function is likely to be good enough for most requirements, | ||
+ | |||
+ | However suppose that instead we want to shuffle a pack of 52 playing cards. Now the number of possible permutations is 52x51x50x....x2 or about 8*10^67! This is a massively larger number than the length of the RND sequence, so only a minuscule fraction of all the possible shuffles can be achieved using RND. There are no ' | ||
===== How to do better than RND ===== | ===== How to do better than RND ===== | ||
- | \\ | + | |
+ | There is no shortage of pseudo-random number generator algorithms with a longer sequence length than RND. For example the assembler-code algorithm [[/ | ||
+ | |||
+ | One thing not to attempt is to write your own algorithm, unless you're a top-notch mathematician! An algorithm with an unintentional bias is only too easy to create and the consequences could be serious. | ||
+ | |||
+ | Using an improved algorithm is only part of the solution. Even if the sequence length is adequate for the task in hand, you must be able to seed it so that a big enough portion of this length is exploited. For example, in the card shuffling case you need to be able to generate a seed with around 230 bits to ensure all possible permutations of cards could, in principle, be generated. Finding an external source of ' | ||
+ | |||
+ | The Windows API function [[http:// | ||
+ | |||
+ | <code bb4w> | ||
DEF FN_RndSecure | DEF FN_RndSecure | ||
LOCAL R%, P% | LOCAL R%, P% | ||
Line 46: | Line 101: | ||
SYS " | SYS " | ||
= R% | = R% | ||
- | CryptGenRandom uses various sources of //entropy// to seed the generator, for example the tick-count since boot time, the current clock time and the Performance Counter.\\ \\ | + | </ |
+ | |||
+ | CryptGenRandom uses various sources of //entropy// to seed the generator, for example the tick-count since boot time, the current clock time and the Performance Counter. | ||
+ | |||
===== Code examples ===== | ===== Code examples ===== | ||
- | \\ | + | |
==== Lottery numbers ==== | ==== Lottery numbers ==== | ||
- | \\ | + | |
+ | The following routine selects and prints six lottery numbers, each in the range 1 to 49: | ||
+ | |||
+ | <code bb4w> | ||
max = 49 | max = 49 | ||
num = 6 | num = 6 | ||
Line 63: | Line 124: | ||
max = max-1 | max = max-1 | ||
NEXT choice | NEXT choice | ||
- | If you want to display the selection in ascending sequence, you can put the numbers in a second array and sort it:\\ | + | </ |
+ | |||
+ | If you want to display the selection in ascending sequence, you can put the numbers in a second array and sort it: | ||
+ | |||
+ | <code bb4w> | ||
INSTALL @lib$+" | INSTALL @lib$+" | ||
- | sort% = FN_sortinit(0, | + | sort%% = FN_sortinit(0, |
max = 49 | max = 49 | ||
num = 6 | num = 6 | ||
Line 79: | Line 144: | ||
NEXT choice | NEXT choice | ||
C% = num | C% = num | ||
- | CALL sort%, | + | CALL sort%%, |
FOR choice = 1 TO num | FOR choice = 1 TO num | ||
PRINT choices(choice) | PRINT choices(choice) | ||
NEXT | NEXT | ||
- | \\ | + | </ |
==== Shuffling a pack of cards ==== | ==== Shuffling a pack of cards ==== | ||
- | \\ | + | |
+ | Notwithstanding the comments made above, this routine uses RND for the convenience of the example. In practice a better random number generator ought to be used: | ||
+ | |||
+ | <code bb4w> | ||
cards = 52 | cards = 52 | ||
DIM pack(cards) | DIM pack(cards) | ||
Line 98: | Line 167: | ||
NEXT I | NEXT I | ||
+ | </ | ||
+ | |||
This uses Durstenfeld' | This uses Durstenfeld' |
notes_20on_20the_20use_20of_20rnd.1522502370.txt.gz · Last modified: 2024/01/05 00:17 (external edit)