9

I have this variable declarations on my program:

X="MAGENTA"
Y="CYAN"
Z="TAN"
A="KHAKI"

Now what I want is to randomly choose one of these and PRINT it. But how to do this?

Nathan Campos
  • 28,769
  • 59
  • 194
  • 300
  • 1
    +1 for Commodore 64 BASIC! Too bad I don't know the answer :-) – Dean Harding Oct 12 '10 at 02:38
  • 1
    Wow, I'd have to go home to dig out my Programmer's Guide to know the right way to use RND, but kudos for a question about C64 BASIC :) – p00ya Oct 12 '10 at 02:46
  • @Dean: Yeah. I'm still a Commodore 64 lover. It's still a great platform, principally if you want to make some old BASIC games **:)** – Nathan Campos Oct 12 '10 at 02:47
  • @p00ya: I'm buying the Programmer's Guide from eBay **;)** – Nathan Campos Oct 12 '10 at 02:48
  • 1
    I was greatly torn as to whether this question deserved an upvote. While certainly clear, it's usefulness was questionable in my mind. However, there _are_ several `commodore` questions (even at least one VIC-20 which pre-dates the C64) and at least one `zx80` (my first "love"), so I guess there's at least a little bit of interest out there. So +1, but with much angst and gnashing of teeth :-) – paxdiablo Oct 12 '10 at 03:10
  • Nathan: If you're making it this week, it won't be an "old" game. – Ken Oct 13 '10 at 00:07
  • @paxdiablo - I was a `DRAGON 64` / `TRS-80 CoCo` kid, so no upvote from me for this one, but I can see where it comes from... ;-) – ysap Nov 02 '10 at 20:21

3 Answers3

5

My BASIC is pretty rusty but you should just be able to use something like:

10 X$ = "MAGENTA"
20 Y$ = "CYAN"
30 Z$ = "TAN"
40 A$ = "KHAKI"
50 N = INT(RND(1) * 4)
60 IF N = 0 THEN PRINT X$
70 IF N = 1 THEN PRINT Y$
80 IF N = 2 THEN PRINT Z$
90 IF N = 3 THEN PRINT A$

or, putting it in a subroutine for code re-use:

  10 X$ = "MAGENTA"
  20 Y$ = "CYAN"
  30 Z$ = "TAN"
  40 A$ = "KHAKI"
  50 GOSUB 1000
  60 PRINT RC$
  70 END

1000 TV = INT(RND(1) * 4)
1010 IF TV = 0 THEN RC$ = X$
1020 IF TV = 1 THEN RC$ = Y$
1030 IF TV = 2 THEN RC$ = Z$
1040 IF TV = 3 THEN RC$ = A$
1050 RETURN

Of course, you probably should be using arrays for that sort of thing so you can just use:

10 DIM A$(3)
10 A$(0) = "MAGENTA"
20 A$(1) = "CYAN"
30 A$(2) = "TAN"
40 A$(3) = "KHAKI"
50 PRINT A$(INT(RND(1)*4))
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 1
    Will those equalities work? If RND generates a flaot, that should be <= 1, <= 2 etc. right? – p00ya Oct 12 '10 at 02:51
  • 1
    Well, that just shows how rusty by BASIC actually is :-) Fixed with an `INT()` wrapped around it. – paxdiablo Oct 12 '10 at 02:53
  • I am not sure about Commodore Basic, but I think in other Basic's the starting index of an array is normally 1, so you should `DIM A$(4)`. If my (rusty) memory serves me well, then in post BASICA/GWBASIC age Basic's, there was an `OPTION BASE` command for determining whether the array starts from 0 or from 1. – ysap Nov 02 '10 at 20:19
  • Yep, I can confirm that GWBASIC had "OPTION BASE" for selecting the array starting point. I don't recall it being limited to 0/1, I believe you could start at any integer that you wanted. I can't remember if the GWBASIC default was 0 or 1... – Brian Knoblauch Feb 28 '11 at 20:10
  • The starting index of arrays of most BASIC dialects I know (and used) is 0, not 1. – blubberdiblub Dec 20 '15 at 06:25
  • 1
    @BrianKnoblauch It is limited to 0 and 1 and can only be used before any array is DIMensioned, so all arrays in the program must have the same base index. Default is 0 like all the previous BASICs by Microsoft. – BlackJack Jun 30 '22 at 06:15
1

The above answer is correct and comprehensive.

This answer, on the other hand, is not, but I was actually doing a little bit of Commodore BASIC last month and decided that string indexing CAN be useful, sometimes, so here's a non-answer that sort of reframes your problem.

100 X$ = "MAGENTACYAN TAN KHAKI " 110 PRINT MID$(X$,INT(RND(1)*4)*7, 7)

This code gets a random int from 0 to 3, then uses that to find the start index into a single string that contains all four entries, each of which is padded out (where necessary) to 7 characters. That padding is needed because the final parameter to MID$ is the length of the substring to be extracted.

WHY BOTHER?

When to consider indexing over an array: (1) when your string data is near-uniform length, and (2) when you have a LOT of little strings.

If those two conditions are true, then the full code, including the data, is more compact, and takes less memory due to allocating fewer pointers.

P.S. Bonus point if you find that I've made an off-by-one error!

rje
  • 254
  • 3
  • 10
  • 1
    Absolutely an array. That's what the REAL answer did in one of his scenarios. Oh, you're asking when to consider indexing over an array. (1) when your string data is near-uniform length, and (2) when you have a LOT of little strings. If those two conditions are true, then the full code, including the data, is more compact, and takes less memory due to allocating fewer pointers. – rje Oct 15 '19 at 17:36
1

Here's another way to do it, using one variable for the output and ON..GOSUB to set it based on a random number in the range [1..4].

10 ON INT(RND(1)*4+1) GOSUB 100,110,120,130
20 PRINT A$
30 END
100 A$ = "MAGENTA":RETURN
110 A$ = "CYAN":RETURN
120 A$ = "TAN":RETURN
130 A$ = "KHAKI":RETURN
BlackJack
  • 4,476
  • 1
  • 20
  • 25
rje
  • 254
  • 3
  • 10