MDV Software Reclamation

Anything QL Software or Programming Related.
stevepoole
Super Gold Card
Posts: 716
Joined: Mon Nov 24, 2014 2:03 pm

Re: MDV Software Reclamation

Post by stevepoole »

Hi Bwinkel67,
Here is a QL random integer program that doesn't lose digits internally..... Just run it to see the successive scatter diagrams.
But there is one limitation : change 'n=1' to 'n=2', to see the XORed result of a full 65537 number cycle ! ( Cure this by reseeding regularly) .
The seeds are shown in Channel two. They pick up a (beeped) random number, albeit slowly, from the (multitasked) sound processor....
Perhaps this might be useable for your QL encryption project ?
Best Wishes,
Steve.
___________________
100 REMark QLrand_bas
110 WINDOW 256,256,256,0: SCALE 256,0,0
120 PAPER 0: INK 4: CLS: CLS#2 :OVER -1
130 rand=get_seed: n=1
140 REPeat r
150 FOR x=1 TO 128*n
160 FOR y=1 TO 128*n
180 CIRCLE QLrand/128, QLrand/128,1
190 END FOR y: END FOR x
210 rand=get_seed: PAUSE 50: CLS
220 END REPeat r
230 :
240 DEFine FuNction QLrand
250 rand = rand * 8421 +1
260 rand = rand - INT(rand/65537) * 65537
270 RETurn rand
280 END DEFine
290 :
300 DEFine FuNction get_seed
310 LOCal t,i,qs,q$
320 t=1: q$='0': FOR i=1 TO 5: q$=q$&r$
330 qs=q$: IF qs>32766: qs=INT(qs/PI)
340 PRINT#2,qs!!: RETurn qs
350 END DEFine
360 :
370 DEFine FuNction r$
380 LOCal loop,j
390 BEEP t,t: t=t+1: IF t>24: t=1
400 REPeat loop
410 FOR j=1 TO 10: IF BEEPING=0: EXIT loop
420 END REPeat loop
430 RETurn '0124356789'(j)
440 END DEFine
450 ::


User avatar
bwinkel67
QL Wafer Drive
Posts: 1202
Joined: Thu Oct 03, 2019 2:09 am

Re: MDV Software Reclamation

Post by bwinkel67 »

Hi Steve,

Note that randomizing the seed is not something you want to do when encrypting since the seed is the key and you must know exactly what it is so you can decrypt (since you want to recreate the sequence in exactly the same way). With a good 16 bit random number generator, you can choose any of the 65,536 values and they will be equally fine. One usually uses a hash function to convert a string of characters, symbols, and numbers (i.e. what we call passwords) into a numeric value.

With regard to the code you showed:

Code: Select all

140 REPeat r
150   FOR x=1 TO 128*n
160     FOR y=1 TO 128*n
180       CIRCLE QLrand/128, QLrand/128,1
190     END FOR y
200   END FOR x
210   rand=get_seed: PAUSE 50: CLS
220 END REPeat r
The call to QLrand is independent of x and y so it can be more clearly written like this:

Code: Select all

140 REPeat r
150   FOR x=1 TO 16384*(n^2): REMark n=1 -> 1, n=2 -> 4, n=4 -> 9
180     CIRCLE QLrand/128, QLrand/128,1
190   END FOR x
210   rand=get_seed: PAUSE 50: CLS
220 END REPeat r
So when n is 2 then you run x 65536 times but call QLrand twice and so you will repeat the cycle the second go around. That is the limit of the 16 bit sequence and the best you can hope for with using it for encryption.

If you want a truly random sequence with no upper limit for the cycle, then you might as well get rid of QLrand and just use get_seed and call it since it is using the beep command and (I assume) gets some truly random set of numbers. Though useless in cryptography, it might be neat for games and such.

I did try the code below but "t" seems to never get bigger than 5 since it is local to trueRand. This caused the function r$ to only return 0 since beep ended on the first go of the for loop.

Code: Select all

5 MODE 4
10 WINDOW #1, 344, 128, 35, 35: SCALE #1, 128, 0, 0: BORDER #1, 1, 7: PAPER #1, 0: INK #1, 7
15 WINDOW #2, 344, 10, 35, 164: PAPER #2, 0: INK #2, 7
20 FOR i = 1 TO 512
25   x = trueRand: y = trueRand
30   INK #1, 4: CIRCLE #1, x/390, y/780, 1
35   PRINT #2, i, x, x/390, y, y/780
40 END FOR i
45 STOP
100 DEFine FuNction trueRand
105   LOCal t, i, qs, q$
110   t=1: q$='0'
115   FOR i=1 TO 5: q$ = q$ & r$
120   qs=q$: IF qs>32766: qs=INT(qs/PI)
125   RETurn qs
130 END DEFine 
150 DEFine FuNction r$
155   LOCal loop,j
160   BEEP t,t
165   t=t+1: IF t>24: t=1
170   REPeat loop
175     FOR j=1 TO 10: IF BEEPING=0: EXIT loop
180   END REPeat loop
185   RETurn '0123456789'(j)
190 END DEFine 
When I moved "t" out of trueRand (removit it from lines 105 and 110) and made it global (added this: 5 MODE 4: t=1) so it would cycle through all 24 values, it started giving me non-zero strings but mostly they were numbers like 100100 or 101011. So it seems to end the "loop" always pretty early and thus not give very random values.

So then I changed the formula for beep to BEEP 2^t, 2^t going up in powers of 2 (and then I had to cap t at 12). So these changes to the code above:

Code: Select all

5 MODE 4: t=1

Code: Select all

105   LOCal i, qs, q$
110   q$='0'

Code: Select all

160   BEEP 2^t, 2^t
165   t=t+1: IF t>12: t=1
This mostly gave me numbers with 0, 1, 2, 3, and 4 in them which is not very random (i.e. 20134). It sometime throws a 5 or 6 or 7 in there but mostly they are small numbers. So it sounds like a great idea but I just don't think it can be pulled off if you can't get through the repeat loop and randomly have j give you higher values for 5, 6, 7, 8, and 9 so you can construct truly a full set of random 5 digits numbers between 00000 and 99999. Now I ran this on QLAY2 (so no sound chip and much faster) so perhaps on a real QL it works better but that doesn't make it very portable.

Below is the plot I got using the powers of 2 which was the best I could get but it really doesn't distribute well. The reason your scatter plots look great is because you are using that nice 16 bit random number generator that we already tested is good. You just reseed it with these bland numbers that aren't all that random but as long as they are off by a digit the 16 bit random number generator will be fine and not repeat the sequence.
beepRandPlot.png
Still, a pretty cool idea and I liked it...why I spend some time digging into it :-) We should really have created a new forum topic in the beginning as my poor little software reclamation project has been completely drowned out by these fun threads. Too late now but I suspect we might have had more participants.


stevepoole
Super Gold Card
Posts: 716
Joined: Mon Nov 24, 2014 2:03 pm

Re: MDV Software Reclamation

Post by stevepoole »

Hi Bwinkel67,
Sorry about the 't' error... the last file I sent was the wrong version ! ( The SAVEd text file did not overwrite as expected ! )
Here is the correct version, showing seed_values and seed scatter_plot, as well as random scatter_plot.
The seeds are not quite perfectly distributed, but fairly good all the same....
Best Wishes,
Steve.
_____________
100 REMark myrand
110 WINDOW 256,256,256,0: SCALE 256,0,0
120 PAPER 0: INK 4: CLS: CLS#2: OVER -1
130 SCALE#2, 256,0,0: n=1: rand=get_seed
140 :
150 REPeat r
160 FOR x=1 TO 128*n
170 FOR y=1 TO 128*n
180 CIRCLE QLrand/128, QLrand/128,1
200 END FOR y
210 rand=get_seed
220 CIRCLE#2,rand/128,get_seed/128,1
230 END FOR x: CLS
240 END REPeat r
250 :
260 DEFine FuNction QLrand
270 rand = rand * 8421 + 1
280 rand = rand - INT(rand/65537) * 65537
290 RETurn rand
300 END DEFine
310 :
320 DEFine FuNction get_seed
330 LOCal t,i,qs,q$
340 t=1: q$='0': FOR i=1 TO 5: q$=q$&r$
350 qs=q$
355 IF qs>32766: qs=INT(qs/'3.333333333')
360 PRINT#0,qs,
370 RETurn qs
380 END DEFine
390 :
400 DEFine FuNction r$
410 LOCal loop,j
420 BEEP t,t: t=t+30
430 REPeat loop
440 FOR j=1 TO 10: IF BEEPING=0: EXIT loop
450 END REPeat loop
460 RETurn '0124356789'(j)
470 END DEFine
480 ::


User avatar
bwinkel67
QL Wafer Drive
Posts: 1202
Joined: Thu Oct 03, 2019 2:09 am

Re: MDV Software Reclamation

Post by bwinkel67 »

Just looking at your code, note that your divisors are wrong. For QLrand the max value is 65537 and to scale for 256 x 256 values you need to divide by 256 not 128 (otherwise you lose half the values). For get_seed, the max is 99999 (5 digits between 0 and 9) and so the divisor needs to be 390 (for 128 you get only about 1/6th the values).

I will try your 30 increment for get_seed (what I call trueRand) to see what it does for me. It grows smaller than the 2^t so at least on QLAY2 it may not work but I will give it a go. Are you running it on a QL or an emulator?


stevepoole
Super Gold Card
Posts: 716
Joined: Mon Nov 24, 2014 2:03 pm

Re: MDV Software Reclamation

Post by stevepoole »

Hi,
The latest program is running under QPC2 : The BEEPING return depends on the computer multitasking. ( Otherwise 'random' numbers can be obtained by slicing the Date$ return, but they will then have an inferior scatter_plot.... ).

I will try your divisor modifications later today.
Regards,
Steve.


stevepoole
Super Gold Card
Posts: 716
Joined: Mon Nov 24, 2014 2:03 pm

Re: MDV Software Reclamation

Post by stevepoole »

Hi Bwinkel67,
Using your code suggestions, plus a few tweaks of mine :
The scatter_plots are now both OK, and the range of random numbers is 65537, with full precision, (no overflows). 't' is now simply 't+1'.
As you say, this might be useful for games etc...
Best Wishes,
Steve.
_____________________
100 REMark myrand2
110 WINDOW 256,256,256,0: SCALE 256,0,0
120 PAPER 0: INK 4: CLS: CLS#2: OVER -1
130 SCALE#2, 256,0,0: n=1: rand=get_seed
140 :
150 REPeat r
160 FOR x=1 TO 128*n
170 FOR y=1 TO 128*n
180 CIRCLE QLrand/256, QLrand/256,1
200 END FOR y
210 rand=get_seed
220 CIRCLE#2,rand/256,get_seed/256,1
230 END FOR x: CLS
240 END REPeat r
250 :
260 DEFine FuNction QLrand
270 rand = rand * 8421 + 1
275 IF rand>1E9: STOP
280 rand = rand - INT(rand/65537) * 65537
290 RETurn rand
300 END DEFine
310 :
320 DEFine FuNction get_seed
330 LOCal t,i,qs,q$
340 t=1: q$='0': FOR i=1 TO 5: q$=q$&r$
350 qs=q$
357 IF qs>65537: qs=INT(qs/1.5E8)
358 IF qs+1==1: GO TO 340
360 PRINT#0,qs,
370 RETurn qs
380 END DEFine
390 :
400 DEFine FuNction r$
410 LOCal loop,j
420 BEEP t,t: t=t+1
430 REPeat loop
440 FOR j=1 TO 10: IF BEEPING=0: EXIT loop
450 END REPeat loop
460 RETurn '0123456789'(j)
470 END DEFine
480 ::


stevepoole
Super Gold Card
Posts: 716
Joined: Mon Nov 24, 2014 2:03 pm

Re: MDV Software Reclamation

Post by stevepoole »

Hi,

Here are two improvements to the mar 08 - 9.39am program :

Line 357 the exact number is the string literal '1.52584860' .... in place of 1.5E8 .... to cater for the full 65537 range.

Line 370 return qs -1 .... to allow a return of '0' .... which the line 157 INT( ) statement made impossible.

These seeded scatter plots now seem to be absolutely correct. So that would appear to verify the proof of concept.

The routines now need tailoring for ordinary (non-demo) use by anyone in their programs, ( I will report back ... )

Steve.


User avatar
bwinkel67
QL Wafer Drive
Posts: 1202
Joined: Thu Oct 03, 2019 2:09 am

Re: MDV Software Reclamation

Post by bwinkel67 »

You have a slight bug, fix the line below accordingly to get a fair distribution.

Change from:

Code: Select all

357 IF qs>65537: qs=INT(qs/1.52584860)

To:

Code: Select all

357 qs=INT(qs/1.52584860)
In other words, you always need to normalize, not only when it is above 65537 to get the accurate distribution otherwise you are skewing the scatter plot. Or get rid of the divisor on qs and just plot your x and y using 390 as your divisor not 256 (in other words, let it range form 0 to 99999 and normalize while plotting, or you can change the scale on your plot to 100 an normalize by dividing by 1000). Six of one, half-dozen of the other, so to speak :-/

Note again though, something similar did not work well on QLAY2 so it might only work on QPC. Plus, it also is a bit slower than your random number generator based on the 8421 number (which we did determine is also sufficiently random and pretty good). I wouldn't worry about the cycle being only 16 bits since you can really only work with 16 bit numbers accurately on the QL. Also, the 99999 range is not even 17 bits so you don't really gain anything with regard to precision on the QPC.

That being said, it is a cool implementation adding some physics and electrons into the equation to get a pure random behavior so I like it for that reason. Just may not be practical for application. Certainly cannot be used for encryption or decryption since you need to make the sequence repeatable but for games and the like just to have the cool factor and saying "true random based on physic" might be fun. Maybe if we can prove that it is uniform on QPC, Q40, Q60, all the emulators, and the BBQL, I might just stick it into my ZX81 Simulator once I get that done at the end of summer since ZX81 BASIC does support RND.


stevepoole
Super Gold Card
Posts: 716
Joined: Mon Nov 24, 2014 2:03 pm

Re: MDV Software Reclamation

Post by stevepoole »

Hi Bwinkel67,
Here is a cleaned up version, with three 'user' routines and a demo. 'ds' allows 1 to 10 digit precision ! Pr prints out the random values.
The scatter_plots are fine, and I have tried to eliminate any bugs. Scaling is automatic. Default is two digit precision for scale of 100...
All calculations are floats... don't try using integers ! Fastest in two-digits, but turn the volume down or get ear-plugs...
Not beta-tested yet, but I have turned it inside-out for many hours, so do try to find any bugs please !
Best wishes,
Steve.
___________________________________________

100 REMark QLrand7_bas (+test) v08_03_20
110 REMark ds=decimals, pr=print_out=0..1
120 WMON: ds=2: IF ds<1 OR ds>10: ds=2
130 n$=FILL$('9',ds): SCALE n$,0,0: pr=0
140 ::
150 REPeat scatter_demo
160 x=R_int(n$): y=R_int(n$): POINT x,y
170 IF pr THEN
180 IF x>y: h=x: x=y: y=h
190 PRINT#2,'R_int'!R_int(x) !x
200 PRINT#2,'R_to '!R_to(x,y)!x!y
210 PRINT#2\\: PAUSE 100
220 END IF : END REPeat scatter_demo
230 :
240 ::::::::::::::::::::::::::
250 :
260 DEFine FuNction RaND_1
270 RETurn seed$/n$
280 END DEFine
290 :
300 DEFine FuNction R_int(nb)
310 IF nb>n$ : PRINT'error%':STOP
320 n=ABS(nb): p=RaND_1
330 s=1: IF n<>nb: s=-1
340 RETurn INT(p*(n+1))*s
350 END DEFine
360 :
370 DEFine FuNction R_to(a,b)
380 IF a>n$ OR b>n$ OR a<-n$ OR b<-n$ THEN
390 PRINT 'out of range%...': STOP
400 END IF : q=seed$
410 IF a=b: RETurn a
420 sa=(a>0)-(a<0): sb=(b>0)-(b<0)
430 IF sa+sb=2: RETurn a+MODs(q,(b-a)+1)
440 IF sa=0 THEN
450 IF sb=0: RETurn 0
460 IF sb=1: RETurn MODs(q,(b-a)+1)+a
470 END IF
480 IF sa=-1 THEN
490 IF sb=-1: RETurn MODs(q, b-a+1)+a
500 IF sb=0: RETurn -MODs(q,-a+1)
510 IF sb=1: RETurn MODs(q,b-a+1)+a
520 END IF
530 END DEFine
540 :
550 DEFine FuNction MODs(ms,md)
560 RETurn ms-(INT(ms/md)*md)
570 END DEFine
580 :
590 DEFine FuNction seed$
600 LOCal t,i,q$
610 t=1: q$='': FOR i=1 TO ds: q$=q$&r$
620 IF pr: PRINT#0,q$,
630 RETurn q$
640 END DEFine
650 :
660 DEFine FuNction r$
670 LOCal loop,j
680 BEEP t,t: t=t+1
690 REPeat loop
700 FOR j=1 TO 10: IF BEEPING=0:EXIT loop
710 END REPeat loop
720 RETurn '0123456789'(j)
730 END DEFine
740 :::::::::::::::::::::


stevepoole
Super Gold Card
Posts: 716
Joined: Mon Nov 24, 2014 2:03 pm

Re: MDV Software Reclamation

Post by stevepoole »

Hi all,
I wrote the program above with QPC2, which produced correct random scatter plots.

But on my QL, under QDOS or SMSQ, the scatter plots were not correct.

This is due to different 'BEEPING' polll return speeds, so there can be no universal program for all platforms...... Sorry !

After much experimentation, I cannot find any simple workarounds, so I shall have to throw in the towel.

Steve.


Post Reply