stringrandombasiccommodore

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 these and PRINT it. But how to do this?


Solution

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