#oneaday Day 906: Programming club

I spent an hour or so this evening doing my own personal little "programming club", by which I mean I sat down in front of the MiSTer, fired up the Atari800 core and just spent some pleasant time tapping things into Atari BASIC, guided by the book Atari Sound and Graphics: A Self-Teaching Guide by Herb Moore, Judy Lower and Bob Albrecht. I am deliberately doing things as I would have back in the day; that means no modern tools (aside from a Kindle Scribe I have the book on), no IDEs and definitely no fucking generative AI.

I picked this book primarily because I recognised Bob Albrecht's name from a book simply called Atari BASIC which I had used back in the day, and because the main things I want to play with at the moment — or the things I want to know more about over the long term — are the graphics and sound capabilities of the Atari 8-bit. I need to revisit some concepts, such as things like redefining character sets and perhaps some of the mathematical functions, but I've been pleasantly surprised quite how much of that knowledge from back in the early to mid '80s I have apparently retained.

I mean, I have retained knowledge and experience to such a degree that I can still remember where certain symbols are on the Atari keyboard when they don't match a modern layout. Almost immediately, I was hitting some sort of long-dormant muscle memory, and it was a nice feeling; like stepping back into a familiar place after being away for a long time.

I genuinely learned something, too. I've always thought I understood the random number command in Atari BASIC, but it turns out I had misunderstood it quite significantly. To be fair, I had only ever seen it used in one particular way, and thus I had naturally come to assume that is how it worked, but today I feel I actually got a proper handle on how it really does work.

What's that I don't hear you saying? You want to know specifics? Well, all right, since you insist. Here's the simplest use of the Atari BASIC random number keyword:

A=RND(1)

This sets the variable A to a value between 0 and 1, including decimal places. The number in the brackets is irrelevant; most people use either 1 or 0. It doesn't matter.

Getting a number with lots of decimal places isn't always convenient, though, so in that case you use the INT keyword to round it off and get an integer or whole number. Thus the following:

A=INT(RND(1))

…sets the variable A to an integer value between 0 and 1, which means it excludes any fractions. In other words, you will get 0 or 1. But what if you want a bigger number? Well then, you just add a multiplication factor (represented as * in BASIC, as in most other programming languages and spreadsheets), but there's a small caveat to this:

A=INT(20*RND(1))

This sets the variable A to one of 20 different possible integer values. However, they are not between 1 and 20; they are between 0 and 19. To get between 1 and 20, you tweak it to this:

A=INT(20*RND(1)+1)

In other words, you're taking a random integer between 0 and 19 — one of those 20 possible values — then adding 1 to it, which results in a random integer between 1 and 20, which is still one of 20 possible values. So far so good, right? When I came across this as a child, I naturally assumed that the "20*" in this example represented the maximum you wanted the random number to be, and the "+1" was the minimum you wanted it to be. But it does not work like that. I mean, it does if you want a random number between 1 and 20, but what if you want a random number from a range that doesn't begin with 1? What if, say, you wanted a random number between 50 and 60?

Using my mistaken and misunderstood theory from childhood, this, then, doesn't work:

A=INT(60*RND(1)+50)

What happens here is you get a random integer between 0 and 59, then you add 50 to it. As you can probably tell, this causes problems at various points in the range. If the initial random number generated was 59, for example, adding 50 to that will give you 109, which is not between the 50-60 range we are after.

Instead what you have to do is set the multiplication factor to the number of possible different results you want, not the maximum value. Then you set the number added to the minimum value. Then it works. Between 50 and 60 we have 11 values, because we count 50 as the first one. So this does work:

A=INT(11*RND(1)+50)

What happens in this instance is you get a random integer between 0 and 10 (one of 11 possible values), then add 50 to it. This results in a random integer between 50 and 60.

What if you want a number between 0 and 50 that is a multiple of 5? Well, we use what we've just learned. The different possible values are 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 — that's 11 of them in total. So we use 11 as our multiplication factor to get one of 11 different possible values. We then multiply the entire equation again by the "step" we want our random numbers to go in as such:

A=INT(11*RND(1))*5

What happens here is that we pick one of 11 different possible random numbers between 0 and 10, then multiply that result by 5. So if the original number was 0, we get 0; if it was 1, we get 5; if it was 2, we get 10… and so on. The end result is 11 different possible results, each of which is a multiple of 5.

This is relatively rudimentary stuff, I know, but plopping my thoughts down on paper like this is really helping me cement the knowledge in my head — plus I'm just pretty chuffed at finally having figured this out, having discovered I had been misunderstanding this function ever since I was a kid. It never really got me into trouble, mind, because most of the time I was just wanting to generate a random number between 1 and x (to simulate a die roll, say) but it feels pretty damned good to have figured it out properly today!

The remainder of my time today was spent noodling around with various uses of the random number facility to generate graphics and sound. A lot of this I already knew, but it was nice to make the knowledge feel a little more secure, and realising quite how much stuff I do remember has already done wonders for my confidence. It's once we start getting into things that I never really understood, or which are much more complicated, that I suspect it's going to get a lot more challenging. But I'm determined to finally conquer the display list and player/missile graphics if it kills me!

It just might take a few sessions and lots of reference material on hand to get there. But I think this is going to be fun journey. (If you want to follow along, I've set up a new category on this blog called Programming Club, where I'll be putting all the updates from these self-study sessions.)


Want to read my thoughts on various video games, visual novels and other popular culture things? Stop by MoeGamer.net, my site for all things fun where I am generally a lot more cheerful. And if you fancy watching some vids on classic games, drop by my YouTube channel.

If you want this nonsense in your inbox every day, please feel free to subscribe via email. Your email address won't be used for anything else.


Discover more from I'm Not Doctor Who

Subscribe to get the latest posts sent to your email.

Leave a Reply

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