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

0 Members and 1 Guest are viewing this topic.

#### Reyth

• Global Moderator
• Hero Member
• Posts: 4393
• Thanked: 1611 times
##### Re: Learn BASIC To Program Your Own Simulations?
« Reply #15 on: August 04, 2015, 11:14:16 PM »
Welcome Heir!

#### Reyth

• Global Moderator
• Hero Member
• Posts: 4393
• Thanked: 1611 times
##### Re: Learn BASIC To Program Your Own Simulations?
« Reply #16 on: August 05, 2015, 03:09:47 AM »
Code: [Select]
`RANDOMIZE TIMERINPUT "Low Number";lnINPUT "High Number";hnrn=INT(RND*hn)+lnPRINT rn`
Ok this is going to be one jam packed lesson!

First let's start off with the "Golden Rule" of programming:

You will NEVER produce perfect code on the first try.

This leads to the "Almost Golden Rule" of programming:

You will (almost) ALWAYS assume your code is perfect on the first try.

When our code fails to work as designed it is called a "bug".  In this case we intended to create a random number from ln to hn but failed to lower the hn calculation to accomodate the addition of ln.  Here is the formula to insure that we get the actual range of numbers we want:

INT(RND * (max - min + 1)) + min

In the above, max=high number and min=low number.  So, what we need to do is replace that section of code with the proper formula exactly like this:

rn=INT(RND * (hn - ln + 1))+ ln

Now this may seem complicated but don't worry, you have the formula that you can refer to at any time.  In fact its right in the F1 section for RND whenever you need it.  The fact that you know this formula exists is all that matters and so you can rest easy about it.

To make sure the use of this formula is clear, let's say we wanted a random number from 18 to 36.  Using the RND formula we would do this:

INT(RND * (max - min + 1)) + min

INT(RND*(36-18+1))+18

Then of course we would resolve the simple math in the parenthesis and have:

INT(RND*19)+18

Now for the flaw that December identified:

INPUT "Low Number";ln

INPUT "High Number";hn

Here we are relying upon the user to cooperate with us and enter a low number that is actually lower than the high number.  We can actually check the data that the user enters to be sure that it meets the criteria we specify.  This is a form of error trapping and the first thing we need to do is to label our lines because we are going to be altering the flow of the code.

When line labeling was in use in the programming community, numbers were seen as the best method; if a line number is lower than a particular section, you know its located above that section and a higher number is located below.  I still do it that way.  The conventional method was to space them apart by 10; so 10,20,30 etc.  The reason for this is that if you need to add lines in a section you have space and can use 5 like 15,25,35 or 2 like 12,14,16 or even 1 if necessary 10,11,12.  QB64 does not require these labels, I just use them because BASIC used to require them and they work well for me.  Here's how it looks:

10 RANDOMIZE TIMER

20 INPUT "Low Number";ln

30 INPUT "High Number";hn

40 rn=INT(RND * (hn - ln + 1))+ ln

50 PRINT rn

The command we will learn to check the numbers entered by the user is called IF...THEN.  This is known as condition testing.  The way it works is:

IF (a condition exists) THEN (perform action)

The condition we want to test is that the low number is lower than the high number.  So we need to first have the user enter both of those numbers and perform our condition test afterwards.  This means the test needs to be between line 30 and line 40, like so:

35 IF ln >= hn THEN CLS:GOTO 20

This reads "if ln is greater than or equal to hn".  If the user entered the numbers properly, ln will be lower than hn.  The commands after THEN are ONLY executed when the condition after IF is true, as follows:

ln=1, hn=10 -- the program proceeds to line 40 because 1 <  (is less than) 10.

ln=10, hn=1 -- the program clears the screen and executes the code located at line 20 because 10 >= 1

ln=10, hn=10 -- the program clears the screen and executes the code located at line 20 because 10 >= 10

As you might have guessed, CLS stands for "clear screen" and will empty the screen of all data.

GOTO tells QB64 to execute the code at the specified line rather than simply continuing to the next line in the code.  We have specified line 20 so the user can re-enter the numbers properly.

Note that NOTHING after THEN will be executed when the condition specified by IF is not true, not even things after a colon!  IF...THEN will ALWAYS proceed to the next line in the code when the condition specified by IF is false.

Now for some practical examples of IF...THEN from our Rstation code:

30 IF rs < s THEN so(sl) = so(sl) + 1: sl = 0: GOTO 70

If rs is less than s, the entire line is executed.  If rs is greater than or equal to s, the rest of the line is skipped.

50 sl = sl + 1: tol = tol + 1: IF sl > ml THEN ml = sl

Here the variables sl & tol are increased by 1 and ml is made to equal sl ONLY IF sl is greater than ml.  The IF... has no effect on commands that come before it on the line, only on commands after it.

90 IF sp = 16000000 THEN 120

This is a fancy way of saying THEN GOTO 120.  When using IF...THEN where you only want to move to another section of code, you can simply say THEN and specify the line of code.  It would be useless to put a colon on line 90 as nothing after the colon would be ever executed because if sp <> (does not equal) 16M, the line is skipped but if sp = 16M the program jumps to line 120 to execute code there which would prevent the colon from being reached.

101 a\$ = INKEY\$: IF a\$ = "q" OR a\$ = "Q" THEN END

This has some additional features which we will discuss in the next lesson!
« Last Edit: August 05, 2015, 03:11:49 PM by Reyth »

#### dobbelsteen

• Hero Member
• Posts: 1578
• Thanked: 559 times
##### Re: Learn BASIC To Program Your Own Simulations?
« Reply #17 on: August 06, 2015, 08:53:24 AM »
I can explain how to program the roulette in excel.  I think it is better to do that in an new thread. Who is interested to start an excel thread.

#### december

• Veteran Member
• Posts: 375
• Thanked: 40 times
• Gender:
##### Re: Learn BASIC To Program Your Own Simulations?
« Reply #18 on: August 06, 2015, 01:36:34 PM »
First

#### Reyth

• Global Moderator
• Hero Member
• Posts: 4393
• Thanked: 1611 times
##### Re: Learn BASIC To Program Your Own Simulations?
« Reply #19 on: August 06, 2015, 02:02:01 PM »
Ok, are you keeping up here too with what has been presented so far?

#### december

• Veteran Member
• Posts: 375
• Thanked: 40 times
• Gender:
##### Re: Learn BASIC To Program Your Own Simulations?
« Reply #20 on: August 06, 2015, 02:37:42 PM »
101 a\$ = INKEY\$: IF a\$ = "q" OR a\$ = "Q" THEN END
means that if user press q - the program closes?

Should we understand:
"Now for some practical examples of IF...THEN from our Rstation code:
30 IF rs < s THEN so(sl) = so(sl) + 1: sl = 0: GOTO 70
If rs is less than s, the entire line is executed.  If rs is greater than or equal to s, the rest of the line is skipped.
50 sl = sl + 1: tol = tol + 1: IF sl > ml THEN ml = sl" ?
« Last Edit: August 06, 2015, 03:15:55 PM by december »

#### Reyth

• Global Moderator
• Hero Member
• Posts: 4393
• Thanked: 1611 times
##### Re: Learn BASIC To Program Your Own Simulations?
« Reply #21 on: August 06, 2015, 03:32:08 PM »
Each one of the examples is only looking at the one line and describes how it works.  Each example describes how the IF...THEN statement operates on that line only.  So the example from line 30 has nothing to do with the example from line 50 but each line is explained by the text below it.

The INKEY\$ example I will cover in the next lesson but yes, that is its function. : )
« Last Edit: August 06, 2015, 03:50:24 PM by Reyth »

#### Reyth

• Global Moderator
• Hero Member
• Posts: 4393
• Thanked: 1611 times
##### Re: Learn BASIC To Program Your Own Simulations?
« Reply #22 on: December 21, 2017, 05:51:10 AM »
Because I dropped the ball on this one, I provide the following link:

https://www.sololearn.com/

Here everyone can learn the programming language of their choice for free!

Thanks to one of the managers at my job for volunteering this info!

#### Jesper

• Hero Member
• Posts: 1454
• Thanked: 753 times
• Gender:
##### Re: Learn BASIC To Program Your Own Simulations?
« Reply #23 on: December 21, 2017, 09:50:30 AM »
I think javascript is a good entry language. It is similar in syntax to many other modern languages, it is useful for the web, run at client or server.  It is free and internet is full of fora which share all we need to know.

Its need only a browser to run, and works on mobiles.

The following users thanked this post: Reyth

#### MrPerfect.

• Hero Member
• Posts: 1814
• Thanked: 997 times
##### Re: Learn BASIC To Program Your Own Simulations?
« Reply #24 on: December 21, 2017, 10:44:41 AM »
Thank you , Reyth, looks cool!!! Programming classes for free is simply super!
I wonder if there are any ruby programmers on forum, l got one idea ld like to try long ego... kind of interactive chart where result numbers reptresented by dots with different colors depending on their "origin"( variable(s)  with few different values).
If anyone would like to to try and do it..  we'll come to contact me. Vb course on offer for free fo who can do it.

The following users thanked this post: Reyth

#### Mike

• Great Contributor
• Posts: 901
• Thanked: 186 times
##### Re: Learn BASIC To Program Your Own Simulations?
« Reply #25 on: December 21, 2017, 11:40:09 AM »
My 2 cents:

The 3 programming languages offered on the site which Reyth linked to are all good in their own way, but C++ and Java are complex and difficult to learn for programming newbies, and Python, although quite easy to learn is slow for simulations because it's interpreted. Javascript is ok but a bit limited (e.g. you can't read and write to files very easily because of security issues). BASIC is, well, basic.

I'm a long time user of Pascal which was originally designed as a teaching language so is easy to learn, read, and write. It's very mature (been around since 1970), but also modern, if that makes sense. There are no problems with performance and you can write any kind of software (you can even program microcontrollers with it). With the Lazarus IDE it's very easy to write GUI software - there's really nothing to compare with it in the free software world.
« Last Edit: December 21, 2017, 11:48:06 AM by Mike »

The following users thanked this post: Reyth

#### Jesper

• Hero Member
• Posts: 1454
• Thanked: 753 times
• Gender:
##### Re: Learn BASIC To Program Your Own Simulations?
« Reply #26 on: December 21, 2017, 01:43:07 PM »
Java is good, a portable language.  Javascript is good for the web, but also for learning, the syntax is similar to C  , Java.
JavaScript is free, and very good to use as education tool.

Java for instance is not a complex language, it has a gigant library, we can use or write our own. What I think is very important is to learn a language which support a graphic interface, no  unneeded prompts.

I have used javascript to write a Roulette game, and we can write files to the server, and upload them from there.
Javascript  can be used as stand alone and write to the local file, not very common but they do it. The Windows script engine can use it.

The following users thanked this post: Reyth

#### Testen109

• New
• Posts: 2
##### Re: Learn BASIC To Program Your Own Simulations?
« Reply #27 on: December 21, 2017, 02:48:14 PM »
Hi, I want program a bot, any advise?
Thanks.

#### Jesper

• Hero Member
• Posts: 1454
• Thanked: 753 times
• Gender:
##### Re: Learn BASIC To Program Your Own Simulations?
« Reply #28 on: December 21, 2017, 04:29:17 PM »
Hi, I want program a bot, any advise?
Thanks.

I think it is easier to just buy one.  A robot must read the screen. If the casino dislike it, they could just change a strategic pixel, and we have to redo a lot of work.

#### Testen109

• New
• Posts: 2
##### Re: Learn BASIC To Program Your Own Simulations?
« Reply #29 on: December 21, 2017, 05:27:58 PM »
Hi Jesper,
Which one do you recommend?