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 1052 times)
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