@ indy
It's a great example.
Especially when it can be different if you decide to expand it beyond just a yes or no switch. Like making it equal the highest spell that can be cast (like if it's 4 the spells 1 through 4 can be cast).
@ I Am Herenow
Thanks.
Yeah it's just a variable name it can be "Zarfenthootal" and it'll still work. "Picked" just seemed more natural and represents if the random number has already been "Picked" before ( 0=Not Picked 1=Picked Already ).
Switches are used when you want to check things. You turn the switch off then do the check. In this case the switch is only turned on when it sees the random number picked is the same as a number in the array. After the check you look to see if the switch is still off (or on... however you want to do it). In this case if the switch is still off then that means the random number picked is not in the array. If the switch is on it doesn't exit the do/loop so it loops again to pick another random number, turn the switch off, check the numbers, then look at the switch again.
To help you understand easier I made something I've been wanting to make for people that want to make tutorials. I did add remarks all over but you don't really have to know what this code is doing. Just run it.
It will show you the code on the left side and the current line thats running in code will show up colored red. On the right side is the array list and shows the variables for the random number and "Picked". It will show the current element being worked on (with "Working>") and show the current element being checked (with "<Checking" when it's in the function).
It'll run slow so you'll have plenty of time to see exactly what the code is doing and what the results of it are. Also I reduced the random number to increase the probability that a random number picked is a number already in the array.
This works in both Classic and Pro:
set display mode 800,600,32
maximize window
sync rate 0
sync on
sync
` Dimensionalize arrays to store the code text and variables (to show them)
dim Code$(40)
dim Variable(3)
Code$(0)="Rem Project: Lottery program"
Code$(1)="Rem Created: 23/12/2006 15:18:13"
Code$(2)="Rem ***** Main Source File *****"
Code$(3)=""
Code$(4)="DIM Ball(6)"
Code$(5)=""
Code$(6)="for t=1 to 6"
Code$(7)="` Make the array = the number returned from Ticket()"
Code$(8)=" Ball(t)=Ticket()"
Code$(9)="next t"
Code$(10)=""
Code$(11)="WAIT KEY"
Code$(12)="end"
Code$(13)=""
Code$(14)="` Pick a random number and check if it hasn't been used already"
Code$(15)="FUNCTION Ticket()"
Code$(16)=""
Code$(17)="do"
Code$(18)=""
Code$(19)=" ` Pick a random number from 1 to 6"
Code$(20)=" a=rnd(5)+1"
Code$(21)=""
Code$(22)=" ` Create a switch (if Picked is still zero after the"
Code$(23)=" ` for/next the number has not been picked before)"
Code$(24)=" Picked=0"
Code$(25)=" ` Check each number in the array"
Code$(26)=" for t=1 to 6"
Code$(27)=" ` If the number picked is already in the array turn"
Code$(28)=" ` the switch on"
Code$(29)=" if Ball(t)=a then Picked=1"
Code$(30)=" next t"
Code$(31)=""
Code$(32)=" ` If the number hasn't been picked already exit the do/loop"
Code$(33)=" ` It only exits if the random number isn't in the array"
Code$(34)=" if Picked=0 then exit"
Code$(35)=""
Code$(36)="loop"
Code$(37)=""
Code$(38)="` Exit the function with the random number"
Code$(39)="ENDFUNCTION a"
DIM Ball(6) : ShowCode(4)
for t=1 to 6
ShowCode(6)
Variable(0)=t
` Make the array = the number returned from Ticket()
ShowCode(8)
Ball(t)=Ticket()
ShowCode(9)
next t
Variable(0)=0
ShowCode(11)
WAIT KEY
ShowCode(12)
end
` Pick a random number and check if it hasn't been used already
FUNCTION Ticket()
ShowCode(15)
do
ShowCode(17)
` Pick a random number from 1 to 6
a=rnd(5)+1
Variable(1)=a
ShowCode(20)
` Create a switch (if Picked is still zero after the
` for/next the number has not been picked before)
Picked=0
Variable(2)=Picked
ShowCode(24)
` Check each number in the array
for t=1 to 6
Variable(3)=t
ShowCode(26)
` If the number picked is already in the array turn
` the switch on
ShowCode(29)
if Ball(t)=a then Picked=1:Variable(2)=Picked
ShowCode(30)
next t
Variable(3)=0
` If the number hasn't been picked already exit the do/loop
` It only exits if the random number isn't in the array
ShowCode(34)
if Picked=0 then exit
ShowCode(36)
loop
` Exit the function with the random number
ShowCode(39)
ENDFUNCTION a
` Show code, arrays, and variables
function ShowCode(Num)
cls
for t=0 to 40
` Change Colors
if t<>Num
` Grey
ink rgb(100,100,100),0
else
` Red (represents current working line)
ink rgb(255,0,0),0
endif
text 0,t*15,Code$(t)
next t
` Make the line
ink rgb(255,255,255),0
line 510,0,510,600
text 590,0,"Ball Array"
for t=1 to 6
ink rgb(255,0,0),0
` Show the which element in the array is being worked on
if Variable(0)=t
a$="Working>"
else
a$=" "
endif
text 520,t*15+15,a$
` Show the current element in the array being checked
if Variable(3)=t
text 695,t*15+15,"<Checking"
endif
` Show the ball array
ink rgb(255,255,255),0
` Had to the lines this way to work in Classic... ugh!
a$=" Ball("+str$(t)+") = "
text 580,t*15+15,a$+str$(Ball(t))
next t
` Show the random number and the current state of "Picked"
text 520,150,"Random Number = "+str$(Variable(1))
text 520,190,"Picked = "+str$(Variable(2))
` Show this text if Picked=1
if Variable(2)=1
ink rgb(255,0,0),0
text 520,220,"Random number already used!"
endif
` Sync the screen
sync
` Wait half a second
wait 500
endfunction
Quote: "Also, what's the command that returns the largest variable from a set group/number of them? Thanks!"
You make a variable to store the current highest number and a for/next loop to look at each number in the array. When a number in the array is higher than the current highest number make the variable equal the highest number. If you want to find it by simply knowing which element in the array is higher you make another variable to represent that number.
` Dimensionalize an array
dim Numbers(19)
` Pick 20 random numbers
for t=0 to 19
Numbers(t)=rnd(100)
next t
` Look at all the numbers
for t=0 to 19
` Check if the number in the array is more than
` the current high number
if Numbers(t)>Highest
` Change the variable "Highest" to equal number in the array
Highest=Numbers(t)
` Change the variable "Num" to equal the element number
` of the current highest number
Num=t
endif
next t
print "Highest Number is colored white:"
print ""
` Go through all the numbers again
for t=0 to 19
` Check if t=Num
if t=Num
` White color
ink rgb(255,255,255),0
else
` Grey color
ink rgb(100,100,100),0
endif
` Show the numbers in the array
print " "+str$(Numbers(t))
next t
wait key