# Thread: Old pseudo random number generator

1. ## Old pseudo random number generator

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_So..._Research_Inst
https://archive.org/details/tandy_bo...e+Library&sin=

how to run these GWBASIC codes:
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
```
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

2. 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\$

3. 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

4. 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
```

5. 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.

6. I suppose they are all correct
It depends on what decimal precision program perform calculations.

7. Originally Posted by ErosOlmi
I suppose they are all correct
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.

8. 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

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.

9. 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.

10. 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.

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•