ABOUT THE GIVEAWAY
In today’s blog post, we’ll dig into a Small Basic sample that will select 10 random winners from folks who took the Small Basic 1.2 Feedback Survey!
Awhile ago we had a promotion to win the Small Basic book I cowrote with Majed Marji:
You can learn more about the book here:
- Learn to Program with Small Basic: An Introduction to Programming with Games, Art, Science, and Math
No Starch Press is giving away 20 books as prizes to that contest!
- 10 PRIZES: 1 Ebook for 10 randomly chosen survey participants
- 10 PRIZES: 1 Printed Book for the Top 10 best feedback answers in the survey
And today we’re announcing the winners!
10 FEEDBACK WINNERS
Here are the 10 winners who gave us great feedback and helped us find missing bugs (chosen by our Small Basic team, who acted as judges):
- David EW
- David P
- Sivaraj A
- Matthew P
- Petr V
- Joseph B
- Garth F
- Pete M
- Tiago B
- David W
Those 10 winners received emails to get their printed books!
Special thanks to all the participants who gave us great feedback! You helped us prioritize our bugs, feature back log, and we added a few bugs and feature ideas to our backlog, based on your feedback! (The good news is that we’re using it as we’re working on Small Basic 1.3, and so this feedback is very valuable.)
Now that we know our 10 Feedback winners, let’s pick the 10 random winners!
SAMPLE TO PICK 10 RANDOM WINNERS
I’m going to do it as a Small Basic sample!
I wrote a Small Basic program to select the winners! I wrote this program in 14 lines of code (click it for a larger version, but be sure to come back):
First, I wrote a line that declares a simple array, which is like having one variable that collects an unlimited number of strings. It collects as many different strings or items of data content that I feed into it.
Here is the line:
contestants = “1=TB;2=KA;3=JN;4=TKG;5=TE;6=JDC;7=DBe;8=RAD;9=CA;10=CB;11=MT;12=JD;13=LM; 14=PH;15=JAu;16=DH;17=TMC;18=JAb;19=GC;20=DMK;21=DL;22=JC;23=DF;24=AHa; 25=GH;26=BR;27=RP;28=WC;29=SP;30=ArR;31=JH;32=AM;33=AmR;34=JI;35=RI;36=DBu; 37=BS;38=JK;39=EN;40=CK;41=AnH;42=OH;43=SG;44=DS;45=KP;46=SM”
That’s all one line! It’s just a long line.
In this case, I assigned 46 strings into an array. Those 46 strings are each just a few letters represent the 46 contestants. I took the total number of people who took the survey, I subtracted any folks who already won the printed books (see 10 Feedback Winners above).
What was left was those 46 contestants. I assigned them to the contestants array.
Now I’m ready to start displaying out the text (yes, already!). I’m beginning with a bit of title text that will tell you what this list is about:
TextWindow.WriteLine(“The Winners are:”)
I used the TextWindow object, since I’m going to be displaying text in the text window. I used the TextWindow object’s WriteLine method to write a line of text in the text window. And the text is “The Winners are:” to let the reader know very easily what this list is all about.
I’m ready for my next line! For my third line, I’m assigning the number variable the value of 1:
number = 1
This is just a very basic variable. I’m going to use this variable in my For loop in order to display the numbers 1-10, one number on each line, to count the 10 winners. These numbers (1-10) are displayed on the left side of the list.
It’s time to start the For loop! The fourth line gets the loop started:
For N = 1 To 10
This loop starts at the number 1 and goes 10 times. That’s all this line means. So whatever is inside this loop gets 10 passes. We need to make each pass count!
The fifth line is the first line in our loop. This line picks one of the 10 winners:
winner = Math.GetRandomNumber(46)
We selected a winner! We use the Math object’s GetRandomNumber method to, yup you guessed it, select a random number. The 46 means the random number will be between 1 and 46. Pretty easy! Then it’s assigned into our new variable, winner. The variable is a container that holds one thing at a time. Now it’s holding the number that corresponds to one of our 46 winners!
Our next three lines are interesting. These lines are just checking to see if the person has been picked yet. We can’t pick someone more than once! That wouldn’t be fair to the other 45 contestants! Check it out; here are lines 6-8 (all are still inside the For loop, so we run this check 10 times, once for each winner selection):
While (contestants[winner] = 0)
winner = Math.GetRandomNumber(46)
EndWhile
What just happened? Well, a While loop runs the loop only as long as the condition is true. The condition is contestants[winner] = 0. This uses the winner number we just randomly assigned (for example, let’s say it picked the number 22 from the choices of 1 to 46). Then it checks into the array we declared (back in line 1), looks at the value of the winner number (in the example, it looks into the #22 spot), and sees if the value for that number is 0. Obviously, none of those strings are the number 0 (and the value of our contestants[22] spot in the array is JC)! So in our first iteration of our For loop, this While loop checks, finds the condition is false (nothing is valued at 0), and it never runs. It just skips ahead.
Then why am I using it? Well, later on I’m assigning values to zero after I select them as the winners! Why? Because this way I make sure we don’t select the same winner twice! I’ll explain this more in a bit.
Now line 9 is a blank space. Why? Sometimes your code just needs a break! Some breathing room so that you can visually see where one thought ends before starting another thought.
Line 10 gets to write out the first line in our list of winners! Finally! Here it is:
TextWindow.WriteLine(number + “. “ + contestants[winner])
We use WriteLine to display another line of text out in the text window. The number variable displays the first number in the list (since this is the first time we’re running the loop, and we’ll run it 10 times). So it displays 1. The plus symbol conctatenates (glues) the period with a space, so it looks like (1. ), Then the plus sign concatenates the value of our first winner (so if it the randomly selected number for winner was 3, then it displays: 1. JC).
We have our first winner, displayed on the line! It’s time to look at our 11th line of code:
number = number + 1
That line just takes the number for the line in the list, and it increments the value to one number higher. For example, we started at number 1, and the first line in the list in the text window, displayed the number 1 (it was 1. JC). Now it’s time to add 1 to the value of the variable number, so that it’s ready to display the number 2 on the second line of our list! Why didn’t we just print out the number 2? Well, we’re putting all this in a For loop so we don’t have to print out 10 different lines of code for these different 10 numbers! We can just add one to the variable number instead. So the next time we run this For loop, the number gets incremented to the value of 3.
Next we’re going to set our winner to the value of 0:
contestants[winner] = 0
Now that our winner is set to the value of 0 (instead of to JC), we won’t select JC again!
Line 13 ends our For loop:
EndFor
And then line 14 gives us a blank line (so we get a nice space in the text window before you get the text, “Press any key to continue…”):
TextWindow.WriteLine(“”)
And that’s our code! We did it in just 14 lines!
The second pass on the For loop
Now let’s take a look at what happens the second time we run our For loop (remember we run it 10 times total):
- On line 5, the variable winner gets assigned a new random number, between 1 and 46.
- On lines 6-8, the While loop checks if the array value is set to 0. If it is, that means the person already won. So line 7 selects a new random winner, from 1-46, and then the While loop checks again to see if that person has won yet. The loop continues until it selects someone who hasn’t won yet. Then it exits the loop and moves on…
- Line 10 displays the line for our second winner. In this case, it displays: 2. PH
- Line 11 assigns number to the value of 3 (to display number 3 on the third list item for the next time we run the loop).
- Line 12 sets the second winner to the value of 0, so we don’t pick that person again!
Then we exit the For loop and go to the third pass! Make sense? If not, leave a comment below!
Get the code!
Want the code so you can play with it?
You can import the program with this code (in Small Basic, click the Import button):
- RVG855
You can find an example run of the program, and get the code, on the online gallery:
And that leads us to the 10 winners selected by the program!
Selecting the Winners:
Here are the results of the program and what I saw in the text window when I ran it:
The Winners are:
1. JC
2. PH
3. CK
4. JN
5. RAD
6. KA
7. MT
8. GC
9. DF
10. DH
Press any key to continue…
Here is the screenshot:
We did it! We selected the 10 random winners using computer programming and by using a method that we know is truly random! =^)
There’s probably a website out there that randomly selects winners for you, but since this is for Small Basic, and the whole point is to learn computer science, I turned it into a Small Basic sample!
10 RANDOM EBOOK WINNERS
Here are our 10 winners:
- John C
- Patteri H
- Carl K
- John N
- Ruth AD
- Karthikeyan A
- Matthew T
- Gary C
- David F
- David H
THANK YOU!
A big thanks go out to all 20 winners and everyone who took the survey! We’ve already put the information to use, and it will be very useful for the future of Small Basic!
And don’t forget to check out the book and see if it’s something that will help you:
Learn to Program with Small Basic: An Introduction to Programming with Games, Art, Science, and Math
Small Basic is the only computer programming syntax/text-based language and IDE/UI that was built specifically for kids and learners!
Keep things Small and Basic!
– Ninja Ed