# Random Switch

## Recommended Posts

Hey all, I'm trying to create a random switch, but it seems to always default to the else case.

```\$z = random(0, 3, 0)
switch \$z
case 1
for \$i = 0 to 12 step -1
clickIt(\$i);
Next
case 2
for \$i = 1 to 12 step 3;
if \$i = 13 then \$i = 2;
if \$i = 14 then \$i = 0;
clickIt(\$i);
Next
case 3
for \$i = 1 to 12 step 2;
if \$i = 13 then \$i = 0;
clickIt(\$i);
next
case else
for \$i = 0 to 12
clickIt(\$i);
next
EndSwitch```

Can you see what may be happening?

##### Share on other sites

random(0, 3, 0)needs to be random(0, 3, 1)

try "msgbox(0,random(0, 3, 1),random(0, 3, 1))" to see why and read the help file.

Edited by DicatoroftheUSA

##### Share on other sites

ah-ah-ah!

Thank you.

One more issue that I'm seein is that my for loops do not seem to bounceback as I would like them to. Ought I change the for loops to while loops and simply run them while i!=12, or can I make the for loops work?

##### Share on other sites

You problem there is your manipulating the variable used in stepping through the for loop, for example:

```For \$x=0 To 5 ;Maybe ok
\$x+=1
Sleep(1000)
ConsoleWrite(@LF&\$x)
Next

For \$x=0 To 5 ;probably bad
\$x-=1
Sleep(1000)
ConsoleWrite(@LF&\$x)
Next```

and the following will simply fail, the help file will explain why.

for \$i = 0 to 12 step -1

and in the case of the following, it will never run 13

for \$i = 0 to 12 step 2

Edited by DicatoroftheUSA

##### Share on other sites

I caught the first for loop error.

I was hoping that it would hit 13, and run through.. as it does for 12; I have changed those two for loops into while loops, and am still having errors. It seems to be cutting out after 12, not just at 12.

```\$z = random(0, 3, 1)
switch \$z
case 1
for \$i = 12 to 0 step -1
clickIt(\$i);
Next
case 2
\$i = 1;
while \$i <> 12;
\$i=\$i+3;
if \$i = 13 then \$i = 2;
if \$i = 14 then \$i = 0;
clickIt(\$i);
wend
case 3
\$i = 1;
while \$i <> 12;
\$i=\$i+2
if \$i = 13 then \$i = 0;
clickIt(\$i);
wend
case else
for \$i = 0 to 12
clickIt(\$i);
next
EndSwitch```

And good it is-- I had \$1 instead of \$i for my for loops: my lack of sleep is becoming apparent.

Thank you for your help, good sir!

## Create an account

Register a new account