BBC BASIC for Windows
« BEGINNER »

Welcome Guest. Please Login or Register.
Apr 5th, 2018, 11:54pm



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

If you require a dump of the post on your message board, please come to the support board and request it.


Thank you Conforums members.

BBC BASIC for Windows Resources
Online BBC BASIC for Windows documentation
BBC BASIC for Windows Beginners' Tutorial
BBC BASIC Home Page
BBC BASIC on Rosetta Code
BBC BASIC discussion group
BBC BASIC for Windows Programmers' Reference

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: BEGINNER  (Read 1050 times)
L GRIFFIN
Guest
xx BEGINNER
« Thread started on: Mar 29th, 2010, 11:10pm »

just learning the ropes,I did this bare bones lottery simulator.
I would appreciate some comments on any basic errors and ways to improve my skills.....thx.
vdu 23,22,1600;900;8,16,16,8
rem text 48 by 20
dim lot(10,6)
dim drww(7)
dim hit(10)
dim ttal(10,7)
dim mwon(10)
gh=1
ttdr=1
for f=1 to10
mwon(f)=1
for g= 1 to 6
lot(f,g)=0
next
next
*font arial,20
proc_setup
off
repeat
proc_draw
proc_logic
proc_scrn
ttdr=ttdr+1
until gh=2
end

defproc_setup
cls
print tab(0,6)"This is a lottery simulator"
input tab(0,15)"how mamy tickets do you want " ticket
print tab(0,8)"now choose 6 different numbers between 1 and 49 for each ticket press enter after each entry "
print tab(0,9)"3 numbers pays 10"
print tab(0,10)"4 numbers pays 50"
print tab(0,11)"5 numbers pays 1000"
print tab(0,12)" 5 numbers plus the bonus ball pays 100000"
print tab(0,13)"6 numbers pays 3 million"

for d=1 to ticket
40 for t=1 to 6
50 input tab(t*4,16+d) lot(d,t)
if lot(d,t)>49 or lot(d,t)<1 then 50
next
if lot(d,1)=lot(d,2)or lot(d,1)=lot(t,3)or lot(d,1)=lot(d,4)orlot(d,1)=lot(d,5)or lot(d,1)=lot(d,6) then 40
if lot(d,2)=lot(d,3)or lot(d,2)=lot(d,4)or lot(d,2)=lot(d,5)orlot(d,2)=lot(d,6) then 40
if lot(d,3)=lot(d,4)or lot(d,3)=lot(d,5)or lot(d,3)=lot(d,6) then 40
if lot(d,4)=lot(d,5)or lot(d,4)=lot(d,6) then 40
if lot(d,5)=lot(d,6) then 40
next
cls
endproc

defproc_draw
100 for a=1 to 7
n=rnd(49)
drww(a)=n
next
if drww(1)=drww(2)ordrww(1)=drww(3)ordrww(1)=drww(4)ordrww(1)=drww(5)ordrww(1)=drww(6)ordrww(1)=drww(7) then100
if drww(2)=drww(3)ordrww(2)=drww(4)ordrww(2)=drww(5)ordrww(2)=drww(6)ordrww(2)=drww(7) then100
if drww(3)=drww(4)ordrww(3)=drww(5)ordrww(3)=drww(6)ordrww(3)=drww(7) then100
if drww(4)=drww(5)ordrww(4)=drww(6)ordrww(4)=drww(7) then100
if drww(5)=drww(6)ordrww(5)=drww(7) then100
if drww(6)=drww(7)then 100
endproc

defproc_logic
for z=1 to ticket
hit=0
for n=1 to 6
if lot(z,1) =drww(n) then hit= hit+1
if lot(z,2) =drww(n) then hit= hit+1
if lot(z,3) =drww(n) then hit= hit+1
if lot(z,4) =drww(n) then hit= hit+1
if lot(z,5) =drww(n) then hit= hit+1
if lot(z,6) =drww(n) then hit= hit+1
next
if hit=1 then ttal(z,1)=ttal(z,1)+1
if hit=2 then ttal(z,2)=ttal(z,2)+1
if hit=3 then ttal(z,3)=ttal(z,3)+1 mwon(z)=mwon(z)+10
if hit=4 then ttal(z,4)=ttal(z,4)+1 mwon(z)=mwon(z)+50 sound 3,-15,96,1
if hit=5 then ttal(z,5)=ttal(z,5)+1 mwon(z)=mwon(z)+1000 proc_bonusball
if hit=6 then ttal(z,6)=ttal(z,6)+1 mwon(z)=mwon(z)+3000000
if hit=7 then ttal(z,7)=ttal(z,7)+1 mwon(z)=mwon(z)+100000
next
endproc

defproc_bonusball
for s=1 to 6
if drww(7)=lot(z,s) then hit=7
next
endproc

defproc_scrn
print tab(12,0)"draw number 1 HIT 2 HIT 3 HIT 4 HIT 5 HIT 6 HIT 5 + BONUS MONEY WON RATIO"
for s= 1 to ticket
print tab(10,1+s) ttdr" " ttal(s,1) ttal(s,2) ttal(s,3) ttal(s,4) ttal(s,5) ttal(s,6)" " ttal(s,7)" " mwon(s)" "int(mwon(s)/ttdr*100)"%"
next
endproc






















User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: BEGINNER
« Reply #1 on: Mar 30th, 2010, 5:39pm »

on Mar 29th, 2010, 11:10pm, Guest-L GRIFFIN wrote:
just learning the ropes,I did this bare bones lottery simulator.
I would appreciate some comments on any basic errors and ways to improve my skills

Be careful what you wish for!

Code:
      vdu 23,22,1600;900;8,16,16,8 

You must have a massive screen! Creating a window this big makes your program very non-portable, and in particular prevents me running it. If you expect other people to run the code, use no bigger than an 800x600 window.

Code:
      *font arial,20 

Beware of DPI issues: http://bb4w.wikispaces.com/Supporting+different+DPI+values

Code:
   40   for t=1 to 6
   50     input tab(t*4,16+d) lot(d,t) 

Needless to say, line numbers (and GOTOs) are highly undesirable. It is never, ever, necessary to use them.

Code:
  100 for a=1 to 7
        n=rnd(49)
        drww(a)=n
      next 

You appear to be selecting 7 'random' numbers, and then trying over and over again until you get a set with no duplicates. Far better to use an algorithm that cannot generate duplicates in the first place; one is listed here: http://bb4w.wikispaces.com/Notes+on+the+use+of+RND
(bottom of the page under Lottery Numbers).

Code:
        for n=1 to 6
          if lot(z,1) =drww(n) then hit= hit+1
          if lot(z,2) =drww(n) then hit= hit+1
          if lot(z,3) =drww(n) then hit= hit+1
          if lot(z,4) =drww(n) then hit= hit+1
          if lot(z,5) =drww(n) then hit= hit+1
          if lot(z,6) =drww(n) then hit= hit+1
        next 

If you're going to use a loop to iterate through the values of n, why not use a nested loop to iterate through the second index of the array too:

Code:
      FOR n=1 TO 6
        FOR m=1 TO 6
          IF lot(z,m) = drww(n) THEN hit = hit+1
        NEXT
      NEXT n 


Richard.
User IP Logged

leslie griffin
Guest
xx Re: BEGINNER
« Reply #2 on: Mar 30th, 2010, 9:25pm »

Thx for the help with the RND selection.I looked at your page on RND and tried my own variation on choosing 6 numbers.
dim drw(7)
cnt=0
dim lot(49)
for a=1 to 49
lot(a)=a
next

repeat
r=rnd(49)
if lot(r) then drw(cnt)=r lot(r)=0 print drw(cnt): cnt=cnt+1
until cnt=7

User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: BEGINNER
« Reply #3 on: Mar 30th, 2010, 10:04pm »

on Mar 30th, 2010, 9:25pm, Guest-leslie griffin wrote:
Thx for the help with the RND selection.I looked at your page on RND and tried my own variation on choosing 6 numbers

I'm no mathematician, but I worry about the effect of that approach (discarding numbers if they've already been chosen) on the statistics. It also has the disadvantage of taking a variable time to complete.

The algorithm I listed more accurately reflects what a genuine lottery machine does - at each stage it makes a selection only from the remaining numbers.

It may not matter for your application, but unless you're very confident about the implications it's safer to stick with tried-and-tested algorithms. Anything to do with 'random' numbers is beset with pitfalls for the unwary.

Richard.
User IP Logged

leslie griffin
Guest
xx Re: BEGINNER
« Reply #4 on: Mar 30th, 2010, 10:31pm »

Thx Richard..Makes sense.
i
I do appreciate that you give your time to answer and give advice to basic,pardon the pun, programmers like myself.
User IP Logged

L GRIFFIN
Guest
xx Re: BEGINNER
« Reply #5 on: Mar 30th, 2010, 11:44pm »

I could not see how your prog. checked for duplicates, I thought I was missing something really deep in the code.
Put the code in a loop and it churned out duplicates at random.
for q= 1 to 40
max = 49
num = 6
dim lotto(max)
for I = 1 to max
lotto(I) = I
next I

for choice = 1 to num
R = rnd(max)
print lotto(R);
lotto(R) = max
max = max-1
next choice
print

next
User IP Logged

Ken Down
Guest
xx Re: BEGINNER
« Reply #6 on: Mar 31st, 2010, 04:04am »

For the sake of readability, use upper case for BASIC keywords and lower case for variables.
Did you realise that you can have more than one DIM statement on a line? eg:
DIMx%(5),a$(3),z%50
Integer variables run just slightly faster than real variables, so numeric variables of the form x% or
draw%(20) are better where speed may be important.
User IP Logged

David Williams
Developer

member is offline

Avatar

meh


PM

Gender: Male
Posts: 452
xx Re: BEGINNER
« Reply #7 on: Mar 31st, 2010, 04:10am »

on Mar 31st, 2010, 04:04am, Guest-Ken Down wrote:
Integer variables run just slightly faster than real variables, so numeric variables of the form x% ... are better where speed may be important.


And the (capital letter) static integer variables (A%, B%, C%, ..., Z%) may be even better in that case.


David.
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: BEGINNER
« Reply #8 on: Mar 31st, 2010, 08:38am »

on Mar 30th, 2010, 11:44pm, Guest-L GRIFFIN wrote:
I could not see how your prog. checked for duplicates

It works exactly like a lottery machine: it picks the first number from the full set of 49, then it picks the second number from the remaining 48, then it picks the third number from the remaining 47, etc. Hence it doesn't generate duplicates.

Quote:
Put the code in a loop and it churned out duplicates at random.

You're right - there was a bug in the code! The line:

Code:
      lotto(R) = max 

should be:

Code:
      lotto(R) = lotto(max) 

Sorry about that.

Richard.
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: BEGINNER
« Reply #9 on: Mar 31st, 2010, 09:01am »

on Mar 31st, 2010, 04:04am, Guest-Ken Down wrote:
Integer variables run just slightly faster than real variables, so numeric variables of the form x% or draw%(20) are better where speed may be important.

Integer variables may run faster than real variables, but that is not a relevant comparison in this case because the OP is not using real variables but variant variables (he is storing integer values in the variables). Integer variables do not run significantly faster than variant variables (and they make the program longer):

Code:
      i = 123456
      i% = 123456
      
      TIME = 0
      FOR I% = 1 TO 1000000
        j = i + i + i + i + i + i + i + i + i + i : NEXT
      PRINT TIME
      
      TIME = 0
      FOR I% = 1 TO 1000000
        j% = i% + i% + i% + i% + i% + i% + i% + i% + i% + i% : NEXT
      PRINT TIME 

It's true that the static integer variables do run faster, but that's a separate issue.

Note that Acorn versions of BBC BASIC don't have variant variables, so with those there may well be a speed advantage in using integer variables rather than real variables. However this group is specific to BBC BASIC for Windows which, like BBC BASIC (Z80) and BBC BASIC (86) has variant variables.

Richard.
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: BEGINNER
« Reply #10 on: Mar 31st, 2010, 09:10am »

on Mar 31st, 2010, 04:04am, Guest-Ken Down wrote:
For the sake of readability, use upper case for BASIC keywords and lower case for variables.

The preferred naming convention is lower case for local (and private) variables, capitals for constants and a mixture of lower and upper case for global variables.

Because BBC BASIC for Windows supports asynchronous interrupts (e.g. ON SYS and ON TIME) it is important that global and local variables never share the same name. Because you don't (in principle) know what variable names are used in libraries and other CALLed or INSTALLed modules, adhering to a strict naming convention is the only guarantee of that.

Richard.
User IP Logged

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

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls