User Tools

Site Tools


notes_20on_20the_20use_20of_20rnd

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
notes_20on_20the_20use_20of_20rnd [2022/07/29 11:41] – old revision restored (2020/07/27 14:49) richardrussellnotes_20on_20the_20use_20of_20rnd [2024/01/05 00:21] (current) – external edit 127.0.0.1
Line 66: Line 66:
 As mentioned above, the sequence-length of BBC BASIC's pseudo-random number generator is 2^33-1 (8589934591), so unless your program calls RND at least that number of times (unlikely!) only part of the sequence will be utilised. Determining the starting point in the sequence is called **seeding** the generator. As mentioned above, the sequence-length of BBC BASIC's pseudo-random number generator is 2^33-1 (8589934591), so unless your program calls RND at least that number of times (unlikely!) only part of the sequence will be utilised. Determining the starting point in the sequence is called **seeding** the generator.
  
-When //BBC BASIC for Windows// is executed the random number generator is seeded from the value of [[http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin7.html#time|TIME]], but since this counts the number of centiseconds since the PC was last restarted it is likely to be quite small compared with the sequence length. Suppose for example the PC has been running for 24 hours, so TIME is approximately 8640000, this corresponds to only about one-thousandth of the overall sequence length! So relying on the automatic seeding will not make good use of the potential performance of RND. +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.
- +
-To improve the performance you should seed the generator yourself, using the **RND(-n)** option. To do better than the automatic seeding you need to choose a value which is highly variable, not predictable and can range over most or all of the available range (-2147483648 to -1). On a Windows PC a suitable source of such a seed is the Performance Counter: a 64-bit integer value which counts at a rate up to the clock speed of the CPU. The following code may be used: +
- +
-<code bb4w> +
-        DIM pc{l%,h%} +
-        SYS "QueryPerformanceCounter", pc{} +
-        seed% = RND(-ABS(pc.l%)-1) +
-</code> +
 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.
  
Line 139: Line 130:
 <code bb4w> <code bb4w>
         INSTALL @lib$+"SORTLIB"         INSTALL @lib$+"SORTLIB"
-        sort% = FN_sortinit(0,0)+        sort%% = FN_sortinit(0,0)
         max = 49         max = 49
         num = 6         num = 6
Line 153: Line 144:
         NEXT choice         NEXT choice
         C% = num         C% = num
-        CALL sort%,choices(1)+        CALL sort%%,choices(1)
         FOR choice = 1 TO num         FOR choice = 1 TO num
           PRINT choices(choice)           PRINT choices(choice)
notes_20on_20the_20use_20of_20rnd.1659094876.txt.gz · Last modified: 2024/01/05 00:16 (external edit)