## Choose For Random Strings In Commodore 64 BASIC

commodore 64 run command

c64 basic keyboard input

c64 random number generator

commodore 64 print command

commodore 64 basic renumber

c64 basic editor

commodore 64 basic, poke

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?

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))

**Choose For Random Strings In Commodore 64 BASIC?,** 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 Remark: This article describes the BASIC-Command RND in BASIC V2 at the Commodore 64. Typ: Function General Programming-Syntax: RND (<Number>) The function RND generates random floating point numbers in the range of 0.0 (inclusive) to 1.0 (exclusive). The argument <number> can be positive, negative or zero.

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!

**RND,** Remark: This article describes the BASIC-Command RND in BASIC V2 at the Commodore 64. With RND(0) the C64 generates the random number from the internal clock, though This is because RND(0) can only generate 256 numbers. Remark: This article describes the BASIC function MID$ in BASIC V2 at the Commodore 64. Typ: String Function General Programming-Syntax: MID$(<string>, <1. integer number - startchar>[, <2. integer number>])

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
```

**[PDF] C64 Users Guide - Advanced Basic,** More detailed information can be found in the COMMODORE64 You can INPUTeither numeric or string variables, and even have the. INPUTstatement prompt need to generate a random number, to simulate the throw of dice, for example. The MID$ String Function. Another vital function is MID$. This function selects a portion of any string it is given for its argument. Type the command: PRINT MID$("ABCOEFG",2,4) The result shows you how MID$ works. In this case it displays a 4 character string, starting at the 2nd character of "ABCDEFG".

**[PDF] Commodore 64 Users Guide,** cational applications to choose from. BUSINESS AIDS Functions: describes the string, numeric, and print functions. VARIABLES. The Commodore 64 uses three types of variables in BASIC. These are The first random number should be Remark: This article describes the BASIC function STR$ in BASIC V2 at the Commodore 64. Typ: String Function General Programming-Syntax: STR$(<numerical>) STR$ is used to converting numerical values or variables into a string. When the number is positive or 0, the first char is a space.

**Shell Scripting Recipes: A Problem-Solution Approach,** randstr—Select. a. String. at. Random. To create a random word, whether for a when a Commodore 64 graced my desktop, I wasted many an hour playing Remark: This article describes the BASIC operator AND in BASIC V2 at the Commodore 64. For information on the assembler/machine language instruction of the same name, see AND (assembler). Type: Boolean/bitwise operator General Programming-Syntax: <Boolean/integer term> AND <Boolean/integer term> AND performs two different functions:

**Game Sound: An Introduction to the History, Theory, and Practice ,** The music for the band's practice session was chosen from a random combination of Cover songs were very popular on the Commodore 64, partly because its and bass and strings and everything into three voices'' (cited in Pouladi 2004). table 2-1. commodore 64 basic keywords basic language vocabulary 31 command abbreviation screen function type abs a idiiii b am numeric and a biiii n aizj asc a eiiiii s a numeric atn abiiii t ad numeric chr$ c idiiii h c [].string close cl eiiiii 0 cl 0 clr c biiii l c 0 cmd ceiiiiim c lsi cont c.bii 0 c 0 cos none cos numeric data d idiiii a

##### Comments

- +1 for Commodore 64 BASIC! Too bad I don't know the answer :-)
- 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 :)
- @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
**:)** - @p00ya: I'm buying the Programmer's Guide from eBay
**;)** - 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 :-) - Will those equalities work? If RND generates a flaot, that should be <= 1, <= 2 etc. right?
- Well, that just shows how rusty by BASIC actually is :-) Fixed with an
`INT()`

wrapped around it. - 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. - 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...
- The starting index of arrays of most BASIC dialects I know (and used) is 0, not 1.
- Why not an array?
- 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.