BBC BASIC implementation of RANDU

Originally posted at the BBC BASIC forum by p_m21987

I've recently been reading about the history of pseudo-random number generators, and I've found RANDU to be particularly fascinating. So tonight for fun, I implemented RANDU in BBC BASIC and made this little graphics demo (it runs without modification in Matrix Brandy, BBC BASIC for Windows and BBC BASIC for SDL 2.0):
      randu_seed%%=1

      n%=1600
      DIM p(n%,2), c(n%)
      FOR i%=0 TO n%
        FOR j%=0 TO 2
          p(i%,j%) = (FNrandu-0.5)*700
        NEXT
        c(i%)=1+FNrandu*14
      NEXT

      DIM r(2,2), r2(2,2), a(n%,2)

      MODE 8
      OFF
      ORIGIN 640,512
      *refresh off
      REPEAT
        MOUSE X%,Y%,B%
        a=X%/640*PI
        a2=Y%/-512*PI
        r(0,0)=COSa: r(2,0)=SINa: r(1,1)=1.0: r(0,2)=-SINa: r(2,2)=COSa
        r2(0,0)=1.0: r2(1,1)=COSa2: r2(2,1)=-SINa2: r2(1,2)=SINa2: r2(2,2)=COSa2
        a() = p() . r()
        a() = a() . r2()
        CLG
        FOR i%=0 TO n%
          GCOL 0, c(i%)
          PLOT 69, a(i%,0), a(i%,1)
        NEXT
        *refresh
        WAIT 1
      UNTIL FALSE

      END

      DEF FNrandu
      randu_seed%% = (65539 * randu_seed%%) MOD 2147483648
      = randu_seed%% / 2147483648
The demo generates a number of points in 3D space, and lets you rotate them around by moving the mouse. From some angles it looks like the points are dispersed randomly... but then when you move the perspective a bit, you'll find that the points all lie on 15 planes. Fascinating!

I've really enjoyed looking into this fascinating and amazing function and learning about the history of it tonight. I hope you will too. Here is the wikipedia link which is a very good read: https://en.wikipedia.org/wiki/RANDU
«1

Comments

  • The code above makes use of 64-bit integers so won't run in Acorn's BASIC 5, but this version should (not tried because it crashes my copy of Red Squirrel):
       10 randu_seed%=1
       20
       30 n%=1600
       40 DIM p(n%,2), c(n%)
       50 FOR i%=0 TO n%
       60   FOR j%=0 TO 2
       70     p(i%,j%) = (FNrandu-0.5)*700
       80   NEXT
       90   c(i%)=1+FNrandu*14
      100 NEXT
      110
      120 DIM r(2,2), r2(2,2), a(n%,2)
      130
      140 MODE 8
      150 OFF
      160 ORIGIN 640,512
      170 REPEAT
      180   MOUSE X%,Y%,B%
      190   a=X%/640*PI
      200   a2=Y%/-512*PI
      210   r(0,0)=COSa: r(2,0)=SINa: r(1,1)=1.0: r(0,2)=-SINa: r(2,2)=COSa
      220   r2(0,0)=1.0: r2(1,1)=COSa2: r2(2,1)=-SINa2: r2(1,2)=SINa2: r2(2,2)=COSa2
      230   a() = p() . r()
      240   a() = a() . r2()
      250   CLS
      260   FOR i%=0 TO n%
      270     GCOL 0, c(i%)
      280     PLOT 69, a(i%,0), a(i%,1)
      290   NEXT
      300   WAIT
      310 UNTIL FALSE
      320
      330 END
      340
      350 DEF FNrandu
      360 LOCAL temp%
      370 temp% = (randu_seed% << 16) AND &7FFFFFFF
      380 temp% = (temp% + randu_seed%) AND &7FFFFFFF
      390 temp% = (temp% + randu_seed%) AND &7FFFFFFF
      400 randu_seed% = (temp% + randu_seed%) AND &7FFFFFFF
      410 = randu_seed% / 2147483648
    
  • Haven't tried the code yet, but an interesting investigation! Thanks for the Basic V version.
  • Soruk
    edited September 2023
    It works perfectly in RPCEmu.

    Edit: I did try the 64-bit version in RPCEmu (Matrix Brandy for RISC OS) - it worked, after I clobbered the *refresh lines. (I might modify the code to recognise but ignore Brandy-implemented *-commands where RISC OS native ones don't exist)
  • Soruk wrote: »
    It works perfectly in RPCEmu.
    As RedSquirrel crashes I've installed RPCEmu instead, but that starts up in the GUI rather than at the command prompt, and I've no idea even how to run BASIC!
  • Press F12 to get a * prompt.
    *Modules will list them, and whichever number is BASIC, do
    *Configure Lang. <number>
  • Soruk wrote: »
    Press F12 to get a * prompt.
    Thank you, except that when I press F12 nothing happens (on this laptop the key is shared with end, if that matters). :(
  • Perhaps you need to use the Fn key at the same time?
  • Soruk wrote: »
    Perhaps you need to use the Fn key at the same time?
    It's end with fn and F12 without fn, neither does anything. The key works because in BB4W and BBCSDL GET returns 156.
  • Soruk
    edited September 2023
    You can middle-click on the Acorn, and select *-commands. If you're running RISC OS 5, it's the RISC OS logo, the right-most one on the taskbar.
  • It's end with fn and F12 without fn, neither does anything.
    Actually if I look closely F12 does do something: a thin white line appears at the extreme bottom of the window. Nothing like tall enough to contain any text, but something does happen!

    Is this, I wonder, related to the fact that when I initially opened RPCEmu it displayed as a tiny window, rather as if it was trying to use the native resolution of the display (in Windows High DPI Scaling is enabled by default so this wouldn't normally happen).

    So I changed the Compatibility settings of the shortcut and now it displays at a sensible size, but F12 produces just the thin white line.
  • Soruk wrote: »
    You can middle-click on the Acorn
    Middle click? How can I do that with a touchpad? Are you saying that to use RPCEmu I need to plug in an external mouse?

    RedSquirrel didn't need any of this complication, and has always been reliable until I got this Windows 11 laptop. Now it reports lots of DirectDraw errors and crashes.
  • RISC OS was written with a three-button mouse in mind, all Archimedes and later were supplied with one.

    When you get that thin line, blindly type "basic", then MODE 0
    Hopefully you should get a screen where you can actually read things....
  • Richard_Russell
    edited September 2023
    Soruk wrote: »
    When you get that thin line, blindly type "basic", then MODE 0
    That was a top tip - it works! MODE 3 is arguably even better for maximum clarity.
    I suppose there's no way of configuring BASIC to start in a particular MODE?

    Edit: Once in BASIC I was able to do *modules and *configure so it's usable now. Thanks.

  • To set your startup mode,
    *Configure Mode 3
  • Soruk wrote: »
    To set your startup mode,
    *Configure Mode 3
    Thanks again, it's now behaving quite like what I was used to with Red Squirrel.

    The one remaining annoyance is that I can't move the mouse cursor into the RPCEmu window - I initially thought that it was hiding the mouse pointer but now it looks like it's not even allowing me to move it there!

    This of course means that I can't control the RANDU program, so I'm back to square one! In the settings I have 'Follow Host Mouse' checked. This must be something silly I'm doing, because you said the program works in RPCEmu.
  • I'm using RPCEmu under Linux.

    Within RPCEmu, do MOUSE ON to show the RISC OS mouse pointer. Does it behave as intended?
  • Soruk wrote: »
    Within RPCEmu, do MOUSE ON to show the RISC OS mouse pointer. Does it behave as intended?
    Sadly no, it makes no difference. What is happening is that as soon as I try to move the mouse into the RPCEmu window it is immediately moved to the bottom left-hand corner of that window, and can only be moved from there in a direction away from the window (left or down).

    In BBC BASIC terms it's similar to the effect this program would have:
    REPEAT
      MOUSE TO 0,0
    UNTIL FALSE
    

    It can't always do that under Windows, since any program using the mouse would be unusable. Perhaps it's another Windows 11 thing (I've tried changing the Compatibility settings to Windows 7 but it doesn't help).

    I'd like to check if it is doing the same thing in the RISC OS desktop but now I've configured it to boot into BASIC I don't know how to get back there!

    Is it possible that RPCEmu thinks that when running BASIC 'full screen' the mouse isn't usable?
  • Type *desktop to enter the desktop, this won't override your boot-to-BASIC configuration.

    Maybe try to disable "Follow host mouse"?
  • Soruk wrote: »
    Type *desktop to enter the desktop, this won't override your boot-to-BASIC configuration.
    OK, that's interesting. The mouse works fine in the RISC OS desktop, I can move it anywhere and the pointer is displayed. But as soon as I press F12 and '*basic' the pointer disappears and I can't move the mouse into the window (even though most of the desktop is still visible).

    It seems quite deliberate, as if BASIC doesn't think the mouse can be used. Might it be to do with the romset I'm using (which is RISC OS 4.02, BASIC 1.19 according to the startup message)?

    I don't suppose it's important, but after typing *desktop I get the warning "Machine startup has not completed successfully: File '@.!Boot' not found".
    Maybe try to disable "Follow host mouse"?
    That simply has the (expected) effect of not even being able to move the mouse in the desktop.
  • I'm using 3.71, though you can also use the IOMD build of RISC OS 5 from riscosopen.org
  • Soruk wrote: »
    you can also use the IOMD build of RISC OS 5 from riscosopen.org
    Any chance of a link? I can find IOMD downloads here but none that include individual ROM images as far as I can see.
  • For RISC OS 3.71, here's the ROM from my installation --> http://www.matrixnetwork.co.uk/riscos-3.71.zip - extract riscos-3.71.rom into your roms directory.

    For RISC OS 5.28, get this Zip https://www.riscosopen.org/zipfiles/platform/riscpc/IOMD-Flash.5.28.zip and copy the file !KinPrg/riscos as riscos-5.28.rom into your roms directory. Run only one or the other - and move the RISC OS 4 ROM you have out of the way.
  • Soruk wrote: »
    move the RISC OS 4 ROM you have out of the way.
    Ah, now I see why I am confused. I have separate ROM files (Ic24.rom, Ic25.rom, Ic26.rom, Ic27.rom) so I assumed that was what I was looking for, I hadn't realised I could use a single file instead. I'll try the 5.28 because I'm anxious about using one of unknown provenance.
  • Fair enough - 3.71 is also downloadable from here --> https://www.4corn.co.uk/articles/rpcemu371win/
  • Soruk wrote: »
    For RISC OS 5.28, get this Zip https://www.riscosopen.org/zipfiles/platform/riscpc/IOMD-Flash.5.28.zip and copy the file !KinPrg/riscos as riscos-5.28.rom into your roms directory
    With 5.28 the behaviour is exactly the same: *basic excludes the mouse from the window, whilst *desktop allows it to enter the window. This is seemingly deliberate, so how is one supposed to run a BASIC program that uses the mouse?
  • That is weird, I don't have this trouble on my Linux setup, and I probably shouldn't try to install it on my work laptop.

    There is a mailing list for it here http://www.riscos.info/cgi-bin/mailman/listinfo/rpcemu but as they don't use GitHub it's not so easy to raise a ticket. Besides this, I don't know what to suggest... sorry.
  • Soruk wrote: »
    I don't have this trouble on my Linux setup
    Possibly Linux (or at least the window manager you're using) doesn't support excluding the mouse from a window, and therefore that 'feature' of RPCEmu doesn't work on that platform.

    It certainly seems to be deliberate; could it be that on a real RISC PC the mouse can't be used when BASIC is running fullscreen, and they're trying to reproduce that behaviour?

    It's only on the rare occasion that I try to write - or in this case modify - a program to run in ARM BASIC V that I've used RedSquirrel, and now RPCEmu, to test it. It's very little loss if the mouse doesn't work.
  • It most certainly can be used on a real RISC PC, my parents have one and I regularly use it to test my stuff out on after putting it through its paces in RPCEmu.
  • Tried to post a reply but was stymied by the 'old problem'

    i6v6uyxp42yv.png
  • The mouse is hidden when you press F12, but if you type BASIC then MOUSE ON it will reappear. This also happens on real hardware.
Sign In or Register to comment.