Jump to content

Limitations of RND() function


Recommended Posts

I've been messing around with the emulator a bit, playing with programs that I used to type in for fun all the time back in the day.

The first computer I ever used on a regular basis was the TRS-80 Model III down at the Radio Shack in a nearby shopping center. And it had some pseudographics commands. SET, and RESET. They're very similar to the PSET command.

The program that I am thinking about at the moment was the one for filling the screen with random pixels until the whole screen was full. It took a while, but eventually, every pixel would be filled in, and at that point I'd hit BREAK and move on to the next activity.

But while the TRS-80 BASIC would eventually fill the screen, X16 BASIC won't. Using exactly the same 128×48 grid that the old Z80 system used, trying to use the RND() function to fill the screen results in a few pixels always being left over.

I know that the available routines have a fairly short periodicity, so I tried to enhance the randomness by recursive randomizing. But still, even using RND(RND(1)*32768)*[number of pixels] for both X and Y, this is the best it will do for a 128×48 grid:


I ran that at top speed for an hour, and that's as far as it got. Those white pixels never get filled in. If I try to fill the entire SCREEN $80 grid of 320×240, it's much worse.

I would use RND(0) as the inner seed, but it's my understanding that it's even more repetitive, at least, going by the C64 Wiki's article on the function.

While I don't know much about the TRS-80 version of the function, and I know it's still a pseudo-random number generator, it seems to be a little better at covering a wider range of values more comprehensively.

Is this just something we're stuck with, and will just have to code our own, or is there a way to shoehorn in a better PRNG algorithm into the stock interpreter? Or is there a workaround to get better results out of the existing RND() code?

Link to comment
Share on other sites

X16 uses the Commodore RND(x) function, which uses that "x" parameter in specific unexpected ways:

https://www.c64-wiki.com/wiki/RND      <-- I use c64-wiki for all my Commodore BASIC questions!

RND(0) is never really a good idea.

RND(-TI) should be done first for a seed... but first, have the user press a key.   This effectively randomizes TI.
RND(1) is then used to fetch your random numbers.  And RND(1) or RND(17) or RND(92), and so on, all do the same thing, so RND(1).


  • By using RND(<positive number>) gives a different random number each time from a predetermined sequence (the sequence number is stored internally).
  • Using RND(<negative number>) jumps to a point in the sequence determined by the particular negative number used. Repeatedly calling RND with the same negative number results in the same result; typical use is to call RND(<negative number>) once and then repeatedly call RND(<positive number>).
  • With RND(0) the C64 generates the random number from the internal clock, though the possible values are limited as the digits of the clock only fall in the range 0-60, so it is not suitable for generating large ranges.


Edited by rje
Link to comment
Share on other sites

That's because the program must now be started from the command line with the switch -rtc in order for the real time clock to function and produce the data for use in TI$.

(IMHO, the RTC being active should be the default state of the emulator—and I'm guessing that it probably will be, once Michael has verified that it works properly and conforms with the behavior of the real hardware.)

  • Like 1
  • Thanks 3
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

Please review our Terms of Use