Shop OBEX P1 Docs P2 Docs Learn Events
A complex twist of the RANDOM function. — Parallax Forums

A complex twist of the RANDOM function.

ERMERM Posts: 34
edited 2006-03-30 07:27 in BASIC Stamp
Hi guys,

I came up with an idea for a program feature and I can picture it in my head working, but I can't get the code worked out right. Here's the function:

I am using the BRANCH function to cycle through different loops of code I have written after each button press.

First press - loop A
Second press - loop B
Third press - loop C
and so on.

The loops will only change when a button is pressed. Now the twist is, is there a way to randomly select from the different loops and have them change loops without pressing the button? Kind of like the description below:

Random: 'name of final loop

For X = 1 to 20 'the number of different loops in my program (loop A, loop B and so on)

RANDOM X 'randomly select from loops 1 to 20

For Y = 1 to 4 'repeat loop 4 times

goto X 'loop which was randomly selected

NEXT 'end of 4 repeats

NEXT 'end of loop selection

GOTO Random 'start over with another loop

I think the tricky part will be at the goto X part (which is the goto loop A or B or C). I'm thinking that if you goto another loop, it won't be able to execute the 1 to 4 command because it will be stuck in the other (loop A, B or whatever) loop until a button is pressed. Am I correct on this or am I missing something that will make it work?

Right now, each loop will run continuously until a button press. I am looking to add the feature of a final loop to randomly run all the other loops 4 times before selecting a different loop after the fourth repetition.

Can this be done, or is this just crazy talk?

Thanks in advance.
Tony

Comments

  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-03-30 07:27
    Tony-

    You're quite close. Just make the following changes.
    You need to replace:

    goto X

    with this:

    ON X GOSUB LoopA, LoopB, LoopC, LoopD, ... etc

    And each of the Loop? routines will look something like this:

    LoopA:

    'Do something

    RETURN

    That should do it for you.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
Sign In or Register to comment.