Exploring The TRS-80’s Color BASIC’s Random Number Function

Although these days we get to tap into many sources of entropy to give a pretty good illusion of randomness, home computers back in the 1980s weren’t so lucky. Despite this, their random number generators were good enough for games and such, as demonstrated by the [CoCo Town] YouTube channel.

The CoCo is the nickname for the TRS-80 Color Computer, which despite its name, shares absolutely nothing with the TRS-80. Its BASIC version is called Color BASIC, which like many others was based on Microsoft BASIC, so the video’s description should be valid for many other BASIC versions as well. In the video we’re first taken through a basic summary of what the floating point format is all about, before running through an example of the algorithm used by Color BASIC for its RND function, using a test program written in Color BASIC.

As described in the video, the used algorithm appears to be the linear congruential generator, which is a pseudo-random generator that requires minimal resources from the hardware it runs on. Of course, its main disadvantage is that it will fairly rapidly begin to repeat itself, especially with a limited number of output bits. This makes it a decent choice even today for something like simple game logic where you just want to get some variation without aiming for cryptographically secure levels of randomness.

Thanks to [Stephen Walters] for the tip.

6 thoughts on “Exploring The TRS-80’s Color BASIC’s Random Number Function

  1. ZX81 BASIC’s Random function is probably simpler (not having watched the video). It’s uint16_t seed=((seed+1)*75) MOD 65537)-1. MOD 65537 is easy to compute:

    uint32_t tmp=((seed+1)*75; // it’s never more than 3 bytes.
    seed=(uint16_t)(tmp)-(tmp>>16);

    Multiplying by 75 is also very easy in Z80 or 6809 Assembly, because 75=1001101₂ .

    ;hl=seed
    inc hl ;seed +1
    xor a
    ld b,a
    ld d,h
    ld e,l ;1
    add hl, hl
    rla ;
    2
    add hl, hl
    rla ;4
    add hl, hl
    rla ;
    8
    add hl, de
    adc a,b ;9
    add hl, hl
    rla ;
    18
    add hl,de
    adc a,b ;19
    add hl, hl
    rla ;
    38
    add hl, hl
    rla ;74
    add hl, de
    adc a,b;
    75
    ld c,a
    scf ;set carry.
    sbc hl, bc ;(seed-(seed>>16))-1.
    ;hl=new seed. 25b, 180 cycles.

    On the 6809 it should be easier, because there’s an 8-bit MUL instruction D=a * b.

  2. A well constructed LCG has full periodicity, now the quality of the randomness is up for debate. A poorly chosen set of constants can cause the LCG to spit out repeating sequences.

    Linear algebra, and matrix multiplication can be used to perform lookaheads on such algorithms. So we can determine the period length without guessing constants and running a full traversal of the LCG internal states.

  3. There is no such thing as “the TRS-80”. Radio Shack had a variety of computers under that brand, from the Model I, II, III, I, 12, Coco, Pocket Computer, MC12, and plenty more. Some were CP/M, some Xenix, Coco supported DOS and OS/9. None of them had sole ownership of the TRS-80 brand.

    1. No, but Tandy did.

      And they chose to call their computers TRS80s. Tandy Radio Shack

      Just like Realistic was their scanner, CB and Scanner line. Receivers as well.

      Stereo Mare for Walkman clones

      Tandy had multiple line names. Thinking every PC Tandy sold in their radio shack stores cant be a trs 80 is like saying every Chevrolet cant also be called a chevy.

Leave a Reply to scott_txCancel reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.