PDA

View Full Version : Old pseudo random number generator



primo
30-06-2018, 17:25
This is an old pseudo random number generator in basic language in the book:
Tandy/Radio Shack Book:: Basic Software Library - Volume 2 (1976)(Scientific Research Inst)
page 389
https://archive.org/details/Basic_Software_Library_Volume_2_1976_Scientific_Research_Inst
all the 8 volumes can be downloaded free here:
https://archive.org/details/tandy_books?and%5B%5D=Basic+Software+Library&sin=

how to run these GWBASIC codes:
download pc-basic from: https://robhagemans.github.io/pcbasic/ a good gwbasic emulator
the program save and load basic files from C:\Users\YourUserName in windows 7. but in winxp it is in the installation folder
the random.bas code: note that ** in the book is replaced by ^

10 REM generates random #'s 0< x <1
20 LET A=4
30 LET A=A/3
50 LET B=(A+R)^8
60 LET R=B-INT(B)
70 LET I=I+1
80 IF I>100 THEN 110
90 PRINT R;
100 GOTO 50
110 LET I=0
120 END

save it to C:\Users\YourUserName in windows 7, the run the pc-basic and load it Load "random.bas"
then Run

here is one possible conversion to thinbasic

Uses "Console"

'generates random #'s 0< x <1
Dim A,B,R As Single
Dim i As Long

A=4
A=A/3
While i<100
B=(A+R)^8
R=B-Int(B)
I=I+1
PrintL Str$(R)+" "
Wend
WaitKey

this is just a showcase but there are too many goodies written for GWBasic over the time from games to math to astronomy... etc and we can use it in the modern Basic languages easily

John Spikowski
01-07-2018, 00:07
Here is how the original looks and runs in Script BASIC.




' generates random #'s 0< x <1
20 SPLIT "4,0,0" BY "," TO A,I,R
30 LET A=A/3
50 LET B=(A+R)^8
60 LET R=B-INT(B)
70 LET I=I+1
80 IF I>10 THEN GOTO 110
90 PRINT FORMAT("%g",R),"\n"
100 GOTO 50
110 LET I=0
120 END


jrs@jrs-laptop:~/sb/examples/test$ scriba random.sb
0.988721
0.238686
0.296148
0.704405
0.294489
0.301148
0.937858
0.994102
0.0343257
0.241162
jrs@jrs-laptop:~/sb/examples/test$

primo
01-07-2018, 09:57
it seems every software have its own way
if we dim thinbasic variables as double then the results will be different after the 5th output from the Script BASIC
also pc-basic results have different outputs except the first output
and this is for the simple rand 1 example in the book.
the same in purebasic we define its variables as double

OpenConsole()
Define.d A,B,R

A=4
A=A/3
While i<10
B=Pow((A+R),8)
R=B-Int(B)
PrintN(StrF(R))
i+1
Wend
PrintN("press enter To exit")
Input()
0.9887212515
0.2386864126
0.2961478829
0.7044042349
0.2939240634
0.1643218696
0.3101503849
0.2259250879
0.9417615533
0.7879283428

matthew
01-07-2018, 15:20
I love these small concise routines, I think that they're far more useful than some of the convoluted programs that get written nowadays. Here's the Basic4GL version...





' Generates random #'s 0< X <1


dim a#, r#, b#, i


a# = 4.0


a# = (a# / 3.0)


newNum:
b# = pow( (a# + r#), 8)
r# = b# - int(b#)
i = i + 1


if i > 100 then goto endProg
endif


printr r#
drawtext()


goto newNum


endProg:
i = 0
end

John Spikowski
01-07-2018, 21:48
Wow!

That's not even close to what TB and SB is returning. (Basic4GL)

Script BASIC numeric variant types are long and real (double). SB does a pretty good job of the transition between them with integer and floating math.

ErosOlmi
02-07-2018, 11:14
I suppose they are all correct :D
It depends on what decimal precision program perform calculations.

John Spikowski
02-07-2018, 16:52
I suppose they are all correct :D
It depends on what decimal precision program perform calculations.

Script BASIC is running this example under Linux with 64 bit precision.

I have tried tweaking different aspects of this script (using FIX() instead of INT(), initializing A and R to floats rather than longs) and it always returns the same results.

ErosOlmi
02-07-2018, 17:54
Important are intermediate calculations language do internally.

I did some tests in Excel to see what it calculates.
Script Basic results are the same as in Excel but different from thinBasic. See image

9859

What I see in Excel is that it makes some rounding starting from the very first number and, due to the kind of algorithm, each step add the difference to the next step till it is quite big.

All internal thinBasic intermediate calculations are made using EXTENDED data type that is 80 bit (10 bytes) floating point data type with 18 digits of precision. Only when result is assigned to the final result it is "truncated" to the final data type.

I do not know other languages.

John Spikowski
02-07-2018, 19:42
I did some tests in Excel to see what it calculates.
Script Basic results are the same as in Excel but different from thinBasic.

I'm happy and will rest my case. :nicethread:

matthew
02-07-2018, 19:47
Well Basic4GL is written in C++ and only uses two numerical datatypes from that language, float and integer. The only time I've ever seen it as a problem was on the old Basic Programming forum where Aurel wrote some Curlicue fractals. When I tried running them in B4GL the images looked different from what everyone else got.

ErosOlmi
02-07-2018, 21:21
ahahahahah :D:D:D

If I use Excel 2013 I get same result as Script Basic
If I use VBA for Excel 2013 I get same result as thinBasic

9860

Also tested Power Basic I get same result as thinBasic

John Spikowski
02-07-2018, 22:07
Too funny!

primo
03-07-2018, 06:39
if we try to see a pattern using what is like Ulam primes spiral, but checking if Round(R,2)= 0.12 as an example we will see a pattern, change 0.12 to 0.13 and another pattern and so on.
but it appears the same number of its 6 digits such as (0.181763) does not recur at all or we need a super computer to check this.
using example primeNumbers in thinbasic examples:

B=(A+R)^8
R=B-Int(B)

If Round(R,2)=0.12 Then
Canvas_Color(Rgb(0, 255, 255))
Canvas_SetPixel(x/3, y/3)

the purpose of (x/3, y/3) is to compress the graph only


Uses "UI"

'-----------------------------------------------------------
' Global variables
'-----------------------------------------------------------
Dim ScreenWidth As Long = 800
Dim ScreenHeight As Long = 800
Dim t1, t2 As Quad '---For time measuring
Dim i, j As Long '---Loopers
Dim x, y, dots As Long '---
Dim turn As Long = 0 '---
Dim num As Long = 1 '---Prime number starting point
Dim points_per_edge As Long = 1 '---
Dim edgecycle As Long = 0 '---

Dim hWin As DWord '---Handle of the canvas window

'-----------------------------------------------------------
' Main program
'-----------------------------------------------------------
hWin = Canvas_Window("experiments ...", 1, 1, ScreenWidth, ScreenHeight )

Canvas_Attach(hWin, 0, %TRUE) ' <- double buffer

timebeginperiod(1)

'---Start timer
HiResTimer_Init
t1 = HiResTimer_Get

'---Init canvas
Canvas_Clear(%BLACK)
Canvas_Box(1, 1, ScreenWidth-1, ScreenHeight-1, 5, Rgb(255,255, 0))

'---Starting point
x = ScreenWidth / 1
y = ScreenHeight / 1
Global A,B,R As Single
'Global A,B,R As Double'Single
A=4
A=A/3

'---Main Prime number looper
For i = 1 To 3000
Plotting()
Next


'---Ending timer
t2 = HiResTimer_Get

Canvas_SetPos 10, ScreenHeight - 20
Canvas_Color(%WHITE, %BLACK)
Canvas_Print "Time taken: " + Format$((t2 - t1) / 1000000, "#.000") + " second - " + _
dots + " dots drawn. Press a key to continue."

'---A last redraw
Canvas_Redraw

timeendperiod(1)

Canvas_WaitKey
Canvas_Window End

'-----------------------------------------------------------
Sub Plotting()
'-----------------------------------------------------------
B=(A+R)^8
R=B-Int(B)

Static IsTimeToRedraw As Long

Incr IsTimeToRedraw
If Mod(IsTimeToRedraw, 20) = 0 Then Canvas_Redraw

turn += 1
If turn = 5 Then turn = 1
edgecycle += 1

If edgecycle = 3 Then
edgecycle = 1
points_per_edge += 1
'Canvas_Redraw
End If

For j = 1 To points_per_edge
num += 1

Select Case turn
Case 1
x = x + 1
Case 2
y = y - 1
Case 3
x = x - 1
Case 4
y = y + 1
End Select

'If IsPrime(num) Then
If Round(R,2)=0.12 Then
Canvas_Color(Rgb(0, 255, 255))
Canvas_SetPixel(x/3, y/3)
dots += 1
End If

Next

End Sub

John Spikowski
03-07-2018, 07:29
I decided to give our little random program a try under Windows 32 bit Script BASIC. I also changed the FORMAT mask to use %f instead of %g.

Not so random to me. :shock01:



' generates random #'s 0< x <1
20 SPLIT "4,0,0" BY "," TO A,I,R
30 LET A=A/3
50 LET B=(A+R)^8
60 LET R=B-INT(B)
70 LET I=I+1
80 IF I>10 THEN GOTO 110
90 PRINT FORMAT("%f",R),"\n"
100 GOTO 50
110 LET I=0
120 END


Windows 32

0.988721
0.238686
0.296148
0.704405
0.294489
0.301148
0.937858
0.994102
0.034326
0.241162


Linux 64

0.988721
0.238686
0.296148
0.704405
0.294489
0.301148
0.937858
0.994102
0.034326
0.241162


This is ProvideX Business BASIC - Windows 32 bit



-:list
0010 REM generates random #'s 0< x <1
0015 PRECISION 6
0020 LET A=4
0030 LET A=A/3
0050 LET B=(A+R)^8
0060 LET R=B-INT(B)
0070 LET I=I+1
0080 IF I>10 THEN GOTO 0110
0090 PRINT R
0100 GOTO 0050
0110 LET I=0
0120 END
-:run
.016866
.045421
.05853
.085489
.421897
.089104
.759626
.201681
.824691
.381424
-:


Here is Minimal BASIC.



C:\bas55v116>bas55
bas55 1.16

This is free software: you are free to change and redistribute it,
but there is NO WARRANTY. Type LICENSE to show the details.

Type HELP for a list of allowed commands.
Ready.
load "random.bas"
random.bas
Ready.
list
20 LET A=4
21 LET R=0
22 LET I=0
30 LET A=A/3
50 LET B=(A+R)^8
60 LET R=B-INT(B)
70 LET I=I+1
80 IF I>10 THEN 110
90 PRINT R
100 GOTO 50
110 LET I=0
120 END
Ready.
run
.988721
.238686
.296148
.704404
.293924
.164322
.31015
.225925
.941762
.787928
Ready.
quit
Discard current program? (y/n) y

C:\bas55v116>