Author Topic: Learn BASIC To Program Your Own Simulations?  (Read 9580 times)

0 Members and 1 Guest are viewing this topic.


Learn BASIC To Program Your Own Simulations?
« on: August 02, 2015, 11:00:11 PM »
I have had this thought about starting a blog on how to do this because there might be some people that would wish to learn how to program if they could.  There may not be any interest in which case I can just let this thread fall out of circulation.

Also, I am not some kind of guru or expert in programming, I just know how.

Additionally, BASIC is considered in the programming community as an inefficient dinosaur that belongs on a museum shelf but not actually to be used for anything.  My personal belief is that BASIC is very easy to learn and use and will be an excellent investment of time if you want to learn how to program, as knowledge of BASIC will help you learn other languages later.

The first step is to get QB64 which is freeware here:

If you have Windows, select that tab to see the download link.  I used the .zip link myself.

When you run it, you will be presented with a blue screen where you can type in BASIC instruction code.

The first lessons will be about a program which I call "Rstation" which will create a loss distribution list for a chosen number of inside betting stations.

Code: [Select]
' Stations/Straight Losses
' 7/58, 10/35?,11/40, 12/31, 13/28, 14/20, 15/20, 16/19, 17/27, 18/11
5 DIM so(500)
7 INPUT "# of stations"; s
20 rs = INT(RND * 37): sp = sp + 1
30 IF rs < s THEN so(sl) = so(sl) + 1: sl = 0: GOTO 70 'output
40 'loss
50 sl = sl + 1: tol = tol + 1: IF sl > ml THEN ml = sl
60 ' output
70 'CLS: PRINT "Spins:"; sp
80 'PRINT "Losses:"; ml
90 IF sp = 16000000 THEN 120
101 a$ = INKEY$: IF a$ = "q" OR a$ = "Q" THEN END
110 GOTO 20

120 cr = cr + 1
130 CLS: PRINT "Completed Results:"; cr
135 IF ml >= oml THEN oml = ml
140 PRINT "Max Loss:"; oml
142 PRINT: GOSUB 200
150 sp = 0: GOTO 20

200 FOR i = 1 TO 456
210 PRINT so(i);: rt = rt + so(i): so(i) = 0: NEXT i: PRINT rt: rt = 0
215 'PRINT tol: tol = 0

You will notice that the first two lines have the " ' " character (apostrophe) as their first character.  This signifies "comment" to QB64; it ignores anything that is typed after an apostrophe.  This way you can type notes to remind you of what a section of code does or for any other reason.

You will also notice numbers that appear first on other lines like 1 5 7 10 20 etc.  Those are simply line labels (names of the line) that can be referred to in the code so the computer can go to a specific line and execute the code there.  In QB64, line labels are not specifically necessary and you can see they weren't even used for the first two lines that I typed in.  QB64 will execute the code line by line, in order, unless told otherwise and because of the ability to tell it otherwise, its a good habit to label lines. Those of you that already know how to program will surely be rolling your eyes at this but its just how I do things. : )  The only thing you cannot do is have 2 lines with the same line label (name).

Let's talk about the heart of the program which is line 20:

20 rs = INT(RND * 37): sp = sp + 1

This line consists of two instructions.  One on each side of the colon.  If you want to put more than one instruction on a single line, the colon is how you separate them so QB64 knows they are two different instructions to be executed sequentially.

Although it might not be apparent, both instructions start out the same way with what is called a variable.  This a label (name) that has a value associated with it that is capable of changing.  The first instruction is rs=INT(RND*37) and the variable here is rs.  The second instruction is sp=sp+1 and the variable there is sp.

The simplest instruction is the second one.  I decided to use the variable sp to track the number of spins.  The first instruction "spins the wheel" (more on that later) and so the very next thing I want to do is have the variable sp count those spins.  The way to do this is to say sp= and then to define what you want it to equal.  Since I am counting spins I want it to equal ONE MORE than it currently does which is sp+1.  So, sp=sp+1 accomplishes this. 

Here is an example.  All variables start with the value of 0 when you first run a program.  So, the first time that line 20 is run sp will be 0.  The first instruction "spins the wheel" (more on that later) and then the second instruction will change the value of sp to 1 because 0+1=1; where 0 was the previous value of sp at program start and that value plus one, is one.  The next time line 20 is executed, sp will be 1 and the same instruction will change the value of sp to 2 (1+1=2).

I would like to explain how the "wheel spins" next (the first instruction) if anyone is interested.  The next lesson will show you how to write your first program using that section of code!
« Last Edit: August 02, 2015, 11:21:09 PM by Reyth »


Re: Learn BASIC To Program Your Own Simulations?
« Reply #1 on: August 03, 2015, 03:58:08 AM »
This is the best idea I've seen for months!
To help us learn just what we need for study roulette, to have direct contact and can ask questions...

Here is your first student.
Thank you


Re: Learn BASIC To Program Your Own Simulations?
« Reply #2 on: August 03, 2015, 04:17:21 AM »
Downloaded - waiting for instructions.
Question: the window can't be maximized?


Re: Learn BASIC To Program Your Own Simulations?
« Reply #3 on: August 03, 2015, 04:22:22 AM »
And if Dobbel follows your example and learn us how to program in Excel...
We'll become very skillful in roulette - if not rich by it!

Thanks again!


Re: Learn BASIC To Program Your Own Simulations?
« Reply #4 on: August 03, 2015, 06:20:24 AM »
Good idea Reyth. If members learned how to code they could test out their own ideas and see for themselves that probability is not just a "theory" but does actually model the real world very well, and you don't need an infinite number of spins to check whether a system works or not.

If don't mind me saying, your coding style is very dated; all those GOTOs make programs hard to read. Programmers learned structured code back in the 1970's. ;-)

An alternative to QB64 is Freebasic. It has a QB compatibility mode which means you can write code in the old QB style, but there are also a lot of new features. It has its own editor/IDE if  you're using windows and there is a larger user base than QB64, so the forum is more active.


Re: Learn BASIC To Program Your Own Simulations?
« Reply #5 on: August 03, 2015, 08:52:55 AM »
60 Years ago I learned my first program language. It was called ALGOL.In a short time the new and better languages followed.
On the first personal computer the commodore 64 the program language was basic. I have programmed alot of technical items for my students.
Visical basic is a very good program but not use friendly for for starting programmers.
My favorite program is excel. I do all my roulette reseurch with this program.
All window computers have this free program.

My roulette sheets ar also free available. Many members have downloaded several sheets. THe response is very poor.

If there is interest  I can step by step explain my sheets with PowerPoint presentation on internet.
The following users thanked this post: Reyth


Re: Learn BASIC To Program Your Own Simulations?
« Reply #6 on: August 03, 2015, 10:58:11 AM »
Downloaded - waiting for instructions.
Question: the window can't be maximized?

You can maximize the window by hitting ALT-ENTER.  However if you want to change the actual dimensions of the coding window, you use the Options & select Display and you can choose height and width.

Mike is right, I am dated and I don't wish to be otherwise.  If you want more modern methods, feel free to follow his recommendations as I am sure they are accurate.  Furthermore, you may wish to know that the modern methods/languages have more of a "free flow" style and will be different than the way that I program here.  It is my opinion that learning basic coding logic here will hold you in good stead if you want to learn the modern methods elsewhere at a later time.

I downloaded Dobble's Excel sheets and he has much wisdom to offer.  I can only recommend learning his methods.

As far as the next lesson, I am just waking up.  I am also moving in the next couple of days (maybe today) and so I will post it as soon as I can.

Of course in the meantime, you can copy-paste the code into the coding window and hit F5 and it will run.  You then can examine the code to see what it did and have an idea of how it works.  If you want to exit the program you can type "q" or "Q" on the keyboard and it will naturally exit; of course you can just "x-out" of the output window at anytime as well.
« Last Edit: August 03, 2015, 11:36:38 AM by Reyth »


Re: Learn BASIC To Program Your Own Simulations?
« Reply #7 on: August 03, 2015, 12:47:20 PM »
What I like most of your idea is to learn basic and essential of programming and if one understand this, then he can maybe move further.

I tried F5 and it asked me number of station - I said 2 and hit Enter and it started counting. Next time I write 12, but I don't know exactly what I should write and what it is counting and how to read results!
I am not complaining, I know we will go step by step, I just describe what I've done.

After I maximized the window with ALT+ENTER, I couldn't minimize it again and I didn't know how to exit, nothing worked - no ALT+ENTER again, no Escape, no X - somehow I closed it with q and q.

Good luck with moving, I know it is big mess in life.


Re: Learn BASIC To Program Your Own Simulations?
« Reply #8 on: August 03, 2015, 12:56:17 PM »
ALT-ENTER is a toggle and should bring you back to the regular screen if you hit it again in both/either the coding screen (blue) or output screen (black).


Re: Learn BASIC To Program Your Own Simulations?
« Reply #9 on: August 03, 2015, 01:03:12 PM »
Yes, I tried it again.
After you press it again 5 times, then it comes back...


Re: Learn BASIC To Program Your Own Simulations?
« Reply #10 on: August 03, 2015, 10:58:06 PM »
20 rs = INT(RND * 37): sp = sp + 1

Ok so continuing now with the first instruction.  The heart of it is RND*37 where RND is a function and *37 is an operation applied to that function. 

RND generates a random number that ranges from 0 to .9999999.  The asterisk (*) is the way to tell QB64 to multiply and the number following it is the actual number that is multiplied with the random number.

Finally INT is the way to tell QB64 that you want everything in the parenthesis to be an integer (i.e. no decimals).

So what you need to actually know, is that this instruction will generate a random number from 0 to 36.

Let me give you another example of how to use RND to generate a number from 1 to 6 like a die:


This will generate a random number from 1-6 and store that number in the variable n.  Adding a number to the result will determine the minimum random number generated; in this case we have +1 and thus the number is 1-6 (instead of 0-5).

This is also an excellent time to show you how to use the built in help files.  Type the above instruction into your blue screen.  Place your mouse anywhere on RND and click so that the cursor is next to two of the letters.  Now hit F1 and you will see a full rundown of how the RND function works.  You can do this for any QB64 command word that you like.

Now, let's not forget the original instruction.  Can you find the variable there?  Yes, it is rs.  The variable rs will hold the results of the "random spin" which is a number from 0 to 36.

Before we leave off the RND function let's explain the line above it (line 10) RANDOMIZE TIMER.  The random function (RND) uses a list of numbers that is 16,777,216 entries long and unless the RANDOMIZE function is used, QB64 will always jump into that sequence in the same place which will generate the same random numbers (this can be useful for testing or other purposes) but for normal usage we want a "full random" selection of numbers.  TIMER simply tells QB64 to use the system clock to generate a random place in the list of numbers used by RND.

Simply, use RANDOMIZE TIMER every time you plan on generating random numbers.  The only thing you must do is make sure this instruction only executes once per program run or you can seriously corrupt your random data.

Ok, I promised you a program in this lesson so we need to tackle a couple of new things too!

7 INPUT "# of stations"; s

Ok, the INPUT command is very useful.  What it does is allow the user to type a number that can then be stored in a variable and used within the program.

In the above example, the variable being used is s.  This is actually a fancy way of applying the INPUT statement that combines a PRINT function by default (more on this below).  The "normal" way of doing this same INPUT statement is as follows:

PRINT "# of stations":INPUT s

The results are essentially the same, the computer will wait for the user to type a number and store the results in the variable s.

The PRINT command allows the programmer (us) to communicate with the user by displaying text messages.  The format is as above where any text you want to display is enclosed in quotes (" [text to be displayed] ").  If you want to display the contents (value) of a variable you do not use quotes and instead just use the variable name.

Here let's try this out.  Type the following into your blue screen:

PRINT "Low Number":INPUT ln

Now hit F5 to run this code and see how it operates. 

Of course you could do it the fancy way:

INPUT "Low Number";ln

Notice that if you do the shortcut way, you must use a semicolon between the quoted text and the variable you wish to use.

Ok so now we know how to use variables, input data from the user, print data to the screen and generate random numbers.  So let's use all these skills to make our first program!

Let's make a program that will:

1) Ask the user for a low number
2) Ask the user for a high number
3) Generate a random number between the low number and high number
4) Print the random number to the screen

So, type the following into your blue screen:


INPUT "Low Number";ln

INPUT "High Number";hn



And of course type F5 to run.

Did you catch how we used the variables hn & ln to make the random number instead of using regular (static) numbers?  This is a VERY important and powerful concept, so be sure to study and understand it!

I don't know if you have spotted it but there is a serious potential flaw in this program.  We are going to address this flaw with a couple of very powerful tools in the next lesson!
« Last Edit: August 04, 2015, 01:25:54 PM by Reyth »


Re: Learn BASIC To Program Your Own Simulations?
« Reply #11 on: August 04, 2015, 05:17:41 AM »
It is working - our first program!

I thought that you misspelled the "hn" instead of "In" and wanted to worn you for mistake, but decided to try it first, and it worked.

Good job.
F1 Help is is too much for me for now, I mean too long explanations.


Re: Learn BASIC To Program Your Own Simulations?
« Reply #12 on: August 04, 2015, 05:20:08 AM »
Would the flaw be that we didn't set limits for Low and High? And that Low should be lower than High?


Re: Learn BASIC To Program Your Own Simulations?
« Reply #13 on: August 04, 2015, 11:49:12 AM »

And YES you found the flaw!  There is another deeper flaw (a bug) that we will talk about as well.

Don't worry about F1 its too much for me too sometimes (as I said I am not a guru/expert).  Other times though, it is the one thing that allows me to accomplish what I need (knowledge is king).  As you progress you will find that it becomes useful when you tend to forget proper syntax (how do I format that again?) or when you need to do something particular but don't know how.  I still use F1 regularly.
« Last Edit: August 04, 2015, 12:54:12 PM by Reyth »

Re: Learn BASIC To Program Your Own Simulations?
« Reply #14 on: August 04, 2015, 09:13:04 PM »
Excellent thread this.
I am learning so much.  8)
The following users thanked this post: Reyth