QL and random numbers

Recently in the Italian mailing list we’ve discussed about the real randomness of the “random” numbers created by QL through its command RND.

Unlike on the ZX Spectrum, whose corresponding function will never generate certain values, on QL the numbers seem really random.

Let’s try with a simple three lines program:

REPeat CR_POINTS
POINT RND(255),RND(255)
END REPeat

This program is supposed to print random points, therefore the displayed rectangle should be completely filled after some time.

Immagine

From first glance it appears that some areas are titally improbable because there are “holesand rows where QL will never print points. In fact, the proposed program is not complete because there is not only POINT, but also other commands and methods to draw points on the QL: PLOT (to be loaded, non-native in Qdos) and BLOCK. A mailing list user (CRX) analyzed this issue and made some tests with the three keywords adding color. We get different results.

100 REMark *** RANDOMNESS ***
110 REMark ~~~~~~~~~~~~~~~~~~
120 MODE 8: PAPER 0
130 WINDOW 512,256,0,0: CLS
140 INK 5: CSIZE 0,1: CURSOR 77,130: PRINT "Point       Block      Plot"
150 :
160 FOR points=1 TO 3000
170   POINT RND(22 TO 40),RND(60 TO 80)
180   INK RND(1 TO 7)
190 END FOR points
200 :
210 FOR blocks=1 TO 5000
220   BLOCK 2,1,RND(218 TO 280),RND(50 TO 100),RND(1 TO 7)
230 END FOR blocks
240 :
250 REMark ** Requires PLOT & DRAW **
260 REMark ~~~~~~~~~~~~~~~~~~~~~~~~~~
270 :
280 FOR plots=1 TO 5000
290   PLOT RND(350 TO 410),RND(50 TO 100)
300   INK RND(1 TO 7)
310 END FOR plots

4casi

In all cases POINT never fills the rectangle but puts the pixels following a sort of grid, both on real QL and on emulator. With  recent Minerva ROM, colors appear anything but random. With older Minerva and JS ROMs  instead, everything seems more random but observing carefully we realize that the pixels are arranged in exactly the same scheme and with the same colors. Since the internal clock of basic QL has no backup battery, it resets every time you turn on the computer; therefore the he RND function, which is given by a count in fractions of a second starting from the QL power on, can be modified with the RANDOMISE statement.

For example, with the command:
RANDOMISE DATE
we get immediatly more random-looking rectangles, avoiding the “rainbow effect”.

As for the holes that visible with POINT compared to other commands, actually it is due to the fact that POINT, unlike BLOCK and PLOT, uses the graphic coordinate system and not the pixels. Therefore it requires coordinates with fractional values also (POINT 3.5,8.5) in order to switch on all the pixels, while RND provides only integers.

 

Photo: red numbers   by DaveBleasdale /  CC BY  / Blurred from original

One thought on “QL and random numbers

  1. Interessante il problema con la Minerva.

    Su QL con ROM Sinclair l’algoritmo usato, se ri-scritto in C invece che in assembler risulterebbe:

    int Rnd(int from, int to)
    {
    seed *= 0x163;
    return from + (int)(((seed >> 16) * (to + 1 – from)) >> 16);
    }

    dove il ‘seed’ iniziale e’ un qualsiasi numero dispari.

    Complimenti per il sito!

Comments are closed.