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):
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
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%% / 2147483648The 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
1 -
Haven't tried the code yet, but an interesting investigation! Thanks for the Basic V version.0
-
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)0 -
-
Press F12 to get a * prompt.
*Modules will list them, and whichever number is BASIC, do
*Configure Lang. <number>0 -
-
Perhaps you need to use the Fn key at the same time?0
-
-
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.0
-
Richard_Russell wrote: »It's end with fn and F12 without fn, neither does anything.
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.
0 -
You can middle-click on the Acorn
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.
0 -
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....0 -
When you get that thin line, blindly type "basic", then MODE 0
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.
1 -
To set your startup mode,
*Configure Mode 30 -
To set your startup mode,
*Configure Mode 3
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.
0 -
I'm using RPCEmu under Linux.
Within RPCEmu, do MOUSE ON to show the RISC OS mouse pointer. Does it behave as intended?0 -
Within RPCEmu, do MOUSE ON to show the RISC OS mouse pointer. Does it behave as intended?
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?
0 -
Type *desktop to enter the desktop, this won't override your boot-to-BASIC configuration.
Maybe try to disable "Follow host mouse"?0 -
Type *desktop to enter the desktop, this won't override your boot-to-BASIC configuration.
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"?
0 -
I'm using 3.71, though you can also use the IOMD build of RISC OS 5 from riscosopen.org0
-
-
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.0 -
move the RISC OS 4 ROM you have out of the way.
0 -
Fair enough - 3.71 is also downloadable from here --> https://www.4corn.co.uk/articles/rpcemu371win/0
-
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
0 -
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.0 -
I don't have this trouble on my Linux setup
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.
0 -
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.0
-
Tried to post a reply but was stymied by the 'old problem'
0 -
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.0