Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Old pseudo random number generator

  1. #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
    all the 8 volumes can be downloaded free here:
    https://archive.org/details/tandy_bo...e+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
    Last edited by primo; 30-06-2018 at 17:28.

  2. #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$
    Last edited by John Spikowski; 01-07-2018 at 07:45.
    ScriptBasic Project Manager
    Project Site
    support@scriptbasic.org

  3. #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. #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
    
    Attached Images Attached Images
    Operating System: Windows 10 Home 64-bit
    CPU: Intel Celeron N4000 CPU @ 1.10GHz
    Memory: 4.00GB RAM
    Graphics: Intel UHD Graphics 600

  5. #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.
    ScriptBasic Project Manager
    Project Site
    support@scriptbasic.org

  6. #6
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    I suppose they are all correct
    It depends on what decimal precision program perform calculations.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  7. #7
    Quote Originally Posted by ErosOlmi View Post
    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.
    ScriptBasic Project Manager
    Project Site
    support@scriptbasic.org

  8. #8
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    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

    Cattura.jpg

    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.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  9. #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.
    ScriptBasic Project Manager
    Project Site
    support@scriptbasic.org

  10. #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.
    Operating System: Windows 10 Home 64-bit
    CPU: Intel Celeron N4000 CPU @ 1.10GHz
    Memory: 4.00GB RAM
    Graphics: Intel UHD Graphics 600

Page 1 of 2 12 LastLast

Similar Threads

  1. Life 3D pseudo - questions.
    By RobbeK in forum TBGL General
    Replies: 12
    Last Post: 06-11-2013, 10:25
  2. Very cool random number website with java app too
    By kryton9 in forum Math: all about
    Replies: 0
    Last Post: 17-06-2012, 00:57
  3. Random Number
    By OneHitWonder in forum thinBasic General
    Replies: 1
    Last Post: 01-07-2010, 23:45
  4. Random number
    By Michael Clease in forum thinBasic General
    Replies: 10
    Last Post: 03-06-2008, 10:08
  5. Generator of random trees
    By Petr Schreiber in forum TBGL Random Trees
    Replies: 1
    Last Post: 07-09-2006, 11:18

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

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