Board Logo
« More confusion with RND »

Welcome Guest. Please Login or Register.
Jan 16th, 2018, 3:41pm


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Please use the forums Search feature before asking.
Please post code using the code box described in Format Your Messages.
This will keep indentation, separate it better form the message and prevent gibberish.
If the code is too long for one post or additional files are needed, upload a ZIP archive to the Just BASIC Files Archive Site.

« Previous Topic | Next Topic »
Pages: 1 2  Notify Send Topic Print
 hotthread  Author  Topic: More confusion with RND  (Read 340 times)
bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: More confusion with RND
« Reply #15 on: Dec 10th, 2017, 1:19pm »

hmm... I guess I better work on my Tour de France metaphor:
Code:
Print "Running 2 timed tests please wait..."
n = 100000
t = time$("ms")
x = 0 : i = 0
while i < n
    i = i + 1
    x = x + i
wend
t2 = time$("ms")
x = 0 : i = 0
[do]
    i = i + 1
    x = x + i
    if i < n then goto [do]
t3 = time$("ms")
print n; " times adding in While... Wend time is ";t2-t
print n; "     times adding in GOTO loop time is ";t3-t2

 


Wow wasn't expecting that much difference!
User IP Logged

B+
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3635
xx Re: More confusion with RND
« Reply #16 on: Dec 10th, 2017, 2:49pm »

some more testing then
Code:
Print "Running 4 timed tests please wait..."
n = 100000
t = time$("ms")
x = 0 : i = 0
while i < n
    i = i + 1
    x = x + i
wend
t2 = time$("ms")
x = 0 : i = 0
[do]
    i = i + 1
    x = x + i
    if i < n then goto [do]
t3 = time$("ms")

x = 0 : i = 0
10
    i = i + 1
    x = x + i
    if i < n then goto 10
t4 = time$("ms")

x = 0
for i = 0 to n
    x = x + i
next
t5 = time$("ms")

print n; " times adding in While... Wend time is ";t2-t
print n; "     times adding in GOTO [do] loop time is ";t3-t2
print n; "     times adding in GOTO 10 loop time is ";t4-t3
print n; "     times adding in FOR loop time is ";t5-t4
 

1) While is slowest in JB/LB.
EDIT: same with do while/loop, do /loop while, do/ loop until.
I wonder if it might be fixed by Carl?
2) indeed JB 2 (as LB 4.5.1) is just a bit faster then JB 1.01
« Last Edit: Dec 10th, 2017, 3:10pm by tsh73 » User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
rtr
Member in Training
ImageImage


member is offline

Avatar




PM


Posts: 42
xx Re: More confusion with RND
« Reply #17 on: Dec 10th, 2017, 5:23pm »

on Dec 10th, 2017, 12:47pm, bplus wrote:
I look at GOTO as training wheels: an excellent device to get a beginner going with the joys of doing something new before acquiring all the experience necessary to use the main tools effectively.

However, using GOTO is a difficult habit to break once you are no longer a beginner. I would also argue that beginners are particularly prone to the kind of bad practice that using GOTO can lead to (i.e. poor program structure - so-called 'spaghetti code').

I remarked that C has GOTO, but when one learns C it is never mentioned in the early stages: it is introduced as an 'advanced' feature, typically when covering exception handling. As a result no C programmer would think of using GOTO to code a simple loop. Beginners today are most commonly introduced to programming using languages like Scratch, Python or Java - languages which do not have GOTO at all!

I was heavily involved in the BBC's Computer Literacy Project of the early 1980s. A major objective of that project was to teach Structured Programming to complete beginners, that is (loosely) programming without the use of GOTO. The Beginners' Tutorial for 'the dialect of BASIC that accompanied that project' wink doesn't mention GOTO at all until Appendix A: 'Line Numbers and the Dreaded GOTO'!

Richard.

User IP Logged

zzz000abc
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 268
xx Re: More confusion with RND
« Reply #18 on: Dec 12th, 2017, 12:20pm »

Quote:
However, using GOTO is a difficult habit to break once you are no longer a beginner.

going to GOTO habit can be broken by proper understanding of loops and functions I think.
till a beginner is acquainted with loops and functions he can have some using GOTO .
User IP Logged

bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: More confusion with RND
« Reply #19 on: Dec 12th, 2017, 12:46pm »

Yes, just give the loop structures a try, you will like, specially with practice!

I don't think I suffered any addiction problem and I started out on GW Basic 'the Italy of spaghetti code' laugh
User IP Logged

B+
fforrett
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 2
xx Re: More confusion with RND
« Reply #20 on: Dec 20th, 2017, 4:29pm »

Try this..

cls

input "Enter a number: "; UserInput

iterations = 0
[CreateNum]
randNum = int(rnd(1) *4) + 1
if randNum <> UserInput then
print "Random number is:"; randNum
'To show the numbers RNG is creating.
iterations = iterations + 1
else
'Now it matches...
goto [Matched]
end if
goto [CreateNum]

[Matched]
print "It took this many tries to match the number: ";iterations

end
User IP Logged

Rod
Administrator
ImageImageImageImageImage


member is offline

Avatar

Graphics = Goosebumps!


PM

Gender: Male
Posts: 3151
xx Re: More confusion with RND
« Reply #21 on: Dec 21st, 2017, 07:05am »

Aw... you used GOTO. Too easy. Although you have used GOTO in a structured manner the test is to see if you can write it without resorting to GOTO. Use the implied GOTOs in DO/LOOP WHILE/WEND or FOR NEXT

Using these kind of loops guarantees structured program flow.
User IP Logged

tenochtitlanuk ( John F)
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 430
xx Re: More confusion with RND
« Reply #22 on: Dec 22nd, 2017, 04:32am »

As a postscript on GOTO, it's well worth checking out Wikipedia on 'spaghetti code' and 'goto'.
I hadn't previously come across the generalisation to the meta-topic of pasta code, specifically ravioli code and lasagna code ( https://ipfs.io/ipfs/QmXoypizjW3WknFiJnKLwHCnL72vedxjQkDDP1mXWo6uco/wiki/Spaghetti_code.html )

I retrospectively apologise for any vermicelli code I've produced in the past, but DO try to break the habit ( vermicelli =worm-form)
User IP Logged

Visit my LB/JB pages at http://www.diga.me.uk/index.html
I use XP and Ubuntu Linux on old machines!
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3635
xx Re: More confusion with RND
« Reply #23 on: Dec 22nd, 2017, 05:54am »

I think (Just)BASIC is not agile enogh for "vermicelli code".
For that, one need true self-modifiable code
wink
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: More confusion with RND
« Reply #24 on: Dec 22nd, 2017, 12:48pm »

Self modifying code, I haven't thought about that in ages!

That would be an interesting challenge to create a working evolving program that does something better and better or learns (extra credit for useful!). Richard has a game from samples that expanded on it's knowledge base which was cool (but it wasn't rewriting itself or should I say the next generation of itself).
« Last Edit: Dec 22nd, 2017, 12:54pm by bplus » User IP Logged

B+
Pages: 1 2  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls