Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Need help with IFs, LOOPs and Not Equals signs (I am a complete newbie)

Author
Message
I Am Herenow
17
Years of Service
User Offline
Joined: 23rd Dec 2006
Location:
Posted: 23rd Dec 2006 18:53
Hello

I am trying to create a program that will give me random lottery tickets. In the lottery that I play, people have to choose six different numbers from 1 to 49 for each ticket. The code I came up with was:



According to my textbook, "<>" means "is not equal to", but, as it turns out, it did not work in the program - DarkBasic could not understand it. Therefore, I assume, it only works in an IF:

...or whatever.

Therefore, how do I make the random number generator give me 6 random numbers that are all different to each other?

As I am asking how to do this, I may as well find a few more (at least vaguely connected) things out:

1) How do I tell DarkBasic that VariableOne does not equal VariableTwo, if not with the "<>" sign?

2) I tried a different approach, using an IF + DO...LOOP combination (which, again, didn't work). Here is a snippet:



And so on. Again, this didn't work, and I'd like a more elegant way of doing this: from what I have read in my textbook, IFs only work with one condition or another. However, how can I make IFs more complicated: how can I make the program start over if any of the numbers are the same as any of the others, and say all that in one IF? Or, conversely, how can I make the program only print WhicheverNumber if it is not equal to any of the numbers printed before it?

3) What does the RANDOMIZE command do? From what I gathered from my textbook, it's meant to give random integers that are all different to each other (exactly what I want) but when I tried
it didn't work, again.

If someone could give me some tips on IFs and LOOPs and all those other things I asked about (especially how to make my lottery program work!), I would very much appreciate it!
I Am Herenow
17
Years of Service
User Offline
Joined: 23rd Dec 2006
Location:
Posted: 23rd Dec 2006 19:33
I am writing this in Dark Basic Professional, by the way.
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 23rd Dec 2006 21:26 Edited at: 23rd Dec 2006 21:27
The < sign means "less than" and the > sign means "more than". You don't just use those signs together "if A => B then..." means "if A is equal to or more than B then...".

A do/loop is used when you want to have a continuous loop... the "do" starts the loop and the "loop" ends it... you can't use more than one "loop" in a single "do".



"Randomize" is used to change the randomization seed. If you use the same seed you'll get the same numbers generated every time. Most people use "randomize timer()" to make any future "rnd" numbers more random. Using the same seed is good for encryption routines because you'll always get the same numbers from the seed.



Here's the same code with "randomize timer()"... "timer()" changes all the time so it makes for a better random number generation.



Here's a basic number picker with checking to see if the numbers have already been picked:

I Am Herenow
17
Years of Service
User Offline
Joined: 23rd Dec 2006
Location:
Posted: 24th Dec 2006 10:23
Thanks, but I don't quite understand a few things:

1) What does the array do in this case? And what do they do in general, what's the point of them?

2) What is the exact code for making sure that two variables aren't the same?

Thanks!
I Am Herenow
17
Years of Service
User Offline
Joined: 23rd Dec 2006
Location:
Posted: 24th Dec 2006 10:24
Thanks, but I don't quite understand a few things:

1) What does the array do in this case? And what do they do in general, what's the point of them?

2) What is the exact code for making sure that two variables aren't the same?

Thanks!
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 24th Dec 2006 19:50 Edited at: 24th Dec 2006 19:52
1.

Arrays are really handy, theyre ways of having multiple variables that you can reference using a number. Whats so useful from using a number? Well, arrays mainly shine when used in things like loops, Ill start off simple.

This is how to declare an array;


DIM myArray(3)


We've named the array "myArray", and given it 3 slots for information.

Now, if we wanted to give each slot of the array's 3 slots different information, we could do this;


myArray(1) = 10
myArray(2) = 20
myArray(3) = 17


Now, our array has different values for each slot. Its also important to know that all arrays have a slot for 0 as well. By this I mean, you can do this;


myArray(0) = 241


The values we fill in each slot can be anything, as long as we declared the array properly. Declaring arrays is just like declaring variables, the type of info they'll be storing needs to be specified. Out array above will hold numbers, but what if we wanted to hold strings?

Its just like declaring a string variable, we use the $ symbol.

DIM names$(2)


We've made a string array called "names", with 2 slots (3 if you count the 0 slot). We could then fill these slots with text;


names$(0) = "Grog Grueslayer"
names$(1) = "RUCCUS"
names$(2) = "I Am Herenow"


Arrays have a lot of other good uses, but at this stage of learning you shouldnt really worry about arrays all that much until you've gotten a bit more experienced with other aspects of programming.

2.
Here's a test program showing how to check if 2 numbers aren't the same.



`Let the user enter 2 numbers, stored in variables num1# and num2#
INPUT "Enter Number 1: ",num1#
INPUT "Enter Number 2: ",num2#

`Now, check if num1# is not equal to number 2 using the <> operator
IF num1# <> num2#
`If they're not equal, print this;
PRINT "Number 1 isnt equal to number 2"
ELSE
`Otherwise (ELSE), if they are equal, print this;
PRINT "Number 1 is the same as number 2"
ENDIF

PRINT "Press any key to end the program"

`Wait for the user to press a key
WAIT KEY

`End the program
END


Using <> will check if two variables arent equal, but there are other methods as well. You could also do one of the following;


IF NOT a = b



IF a = b
do something
ELSE
do something else, since they must not be equal
ENDIF



IF a < b OR a > b


And if I remember correctly, theres one last one (I rarely use it so I might be wrong);


IF a .= b


Hope that helps clear things up. For some reason the code tags arent working... Ohwell.

- RUC'

TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 25th Dec 2006 00:37
@Ruccus

I had the same problem the other day with tags. I discovered that I had one tag which was incorrectly closed. As a result, none of them worked.

If you check your post and find an incorrect one, fix it and all the other tags should then work.

It did for me...

TDK_Man

RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 25th Dec 2006 11:36
Thanks TDK, but I cant seem to find any unclosed tags, unless theres one hidden in one of my paragraphs, all my self-typed onces are closed.

Mr X
19
Years of Service
User Offline
Joined: 25th Sep 2005
Location: Universe, milkyway, sol-system, Earth...
Posted: 25th Dec 2006 15:59 Edited at: 25th Dec 2006 16:01
Quote: "1) What does the array do in this case? And what do they do in general, what's the point of them?"


The point is so you can easy handle larger numbers of data. Of you make a program where you are supposed to manage an hotell with 100 rooms, it's very usefull to write
rather then
.

Another thing with arrays is that you dynamically can change their size. This is very usefull, if you need to add or remove data. As in an RTS where you build units, and then send them to battles where they die.

However, as ROCCUS said, do not worry so much about arrays at the moment. They will come in time, when you are ready.
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 25th Dec 2006 23:32 Edited at: 25th Dec 2006 23:35
Quote: "...to manage an hotell with 100 rooms..."

Quote: "dim rooms( 100 )"


Actually Dim Rooms(100) gets you 101 array elements (0 to 100, inclusive). Not trying to be a git, just pointing that out to newbs who may read this - the zero element counts in DBP arrays!

If you choose a coding style that uses count over index (ie always ignoring index 0) then that's one's choice, and thats fine if it works for you, but I would always try and point out the actuality that the zero index exists, and can be used if need be.

Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 26th Dec 2006 05:59
Quote: "1) What does the array do in this case? And what do they do in general, what's the point of them?"


Ruccus covered it well so I won't go into details again. Lets just say arrays are as important in programming as oxygen is to us.

Quote: "2) What is the exact code for making sure that two variables aren't the same?"


The line "if Numbers(t)=a then Picked=1". But that alone isn't going to do it because it needs the other parts to work properly.

I'll try to explain so you can visually see it on paper as well. Take a piece of paper and write numbers 0 to 5 on it (going down the page). Grab a coin (doesn't matter which). The paper is the array storing 6 numbers. The coin is the "switch"... heads = on and tails = off.

Do this till you get 6 numbers on the paper:

>do
> a=rnd(48)+1

Pick a random number in your head.

> Picked=0

Turn the coin to tails.

> for t=0 to 5
> if Numbers(t)=a then Picked=1
> next t

Look at number 0. If it has a number does it equal the number you picked in your head? (if they are equal turn the coin to heads up)... if it doesn't (or is blank) go to the next number and so on till you look at each number.

> if Picked=0
> Numbers(CNum)=a
> inc CNum
> endif

Now look at the coin. If it's heads up the number in your head is already on the paper... so don't write down the number. If the coin is tails then write down the number on the paper in the first blank spot. CNum is a standard variable that starts at zero (all variables that are used for the first time without defining start at zero) and increases as numbers are picked (it only increases by 1 when the coin is tails).

> if CNum=6 then exit

Now look at your paper. If all 6 numbers are filled stop writing numbers.

> loop

Do it again and again till all numbers are filled. If you pick the same numbers on purpose you'll see what I mean better.

That's basically what the code is doing. It sets a switch to off (zero) looks at each number in the array and if it sees the same number it turns the switch on (one). If the switch is still off then it adds the number to the array and goes to the next spot in the array (CNum).



Quote: "If you choose a coding style that uses count over index (ie always ignoring index 0) then that's one's choice, and thats fine if it works for you, but I would always try and point out the actuality that the zero index exists, and can be used if need be."


I sometimes use zero to store the max number in the array if I dimensionalize the array huge but only use a little bit at first. It saves on having to make another variable just to store the max number of records in the data. When you want to know how many records are in the array you know exactly where to look... the array itself.

I Am Herenow
17
Years of Service
User Offline
Joined: 23rd Dec 2006
Location:
Posted: 26th Dec 2006 16:39
Thanks a lot guys
I Am Herenow
17
Years of Service
User Offline
Joined: 23rd Dec 2006
Location:
Posted: 26th Dec 2006 17:26
Aargh, it still does not work!

OK, here is the program now:



I don't really know what to do with the parameters (or, indeed, what they mean) and so don't know whether it should just be ENDFUNCTION or ENDFUNCTION Ball(6) or ENDFUNCTION Ball(1), Ball(2)...Ball(6) or whether I should have Ticket() or Ticket(Ball) etc.

So if someone could help me on that (I'm pretty rubbish at this whole programming thing lol) it would be very much appreciated! Because I *think* the problem lies with the parameters - when I launch the program, it just launches for 1 second as a blank black screen and closes itself again :S
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 26th Dec 2006 22:41
It doesn't work because all it does it pick random numbers then quits the program with an error. It quits because it hits the function. Functions need to be at the end of the code and anything not in a function needs to be above the functions. It's ok to have remarks between functions.

You need to look at for/next loops to reduce code. It may not seem important right now because your program is small already but for/next loops allow us to do the same thing multiple times with less code.

This:


Does exactly the same as this:


In your code you don't need "print Ticket()" because inside the function you print everything. The only time you would want to add "print" is if your function returns text or a number.

Like this:



It's very, very, VERY bad to call a function within a function your already in. If you want a function that'll check all the numbers you need to make some major changes.

Agent766
17
Years of Service
User Offline
Joined: 23rd Dec 2006
Location: Even I don\'t know.
Posted: 27th Dec 2006 00:17
A way might be:


There are 3 kinds of people: Ones who can count, and ones who can't.

Did you know four out of five people have trouble with fractions?
indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 27th Dec 2006 06:36
the zero location can be used as a flag switch in some cases as well.
Imagine 1 to 10 spells but if zero location = 0 no spells would work.
if zero location is one spells would work etc.
probably a bad example but you get the point.

I Am Herenow
17
Years of Service
User Offline
Joined: 23rd Dec 2006
Location:
Posted: 27th Dec 2006 18:55
Thanks guys, especially Grog.

Also, Grog: your program does work, but I don't quite understand why: where did you get the "Picked = 0" from? Is "Picked" just a random variable or is it some in-built function in DBP?

Basically, how do switches work? Thanks
I Am Herenow
17
Years of Service
User Offline
Joined: 23rd Dec 2006
Location:
Posted: 27th Dec 2006 19:35
Also, what's the command that returns the largest variable from a set group/number of them? Thanks!
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 29th Dec 2006 04:18
@ indy

It's a great example. Especially when it can be different if you decide to expand it beyond just a yes or no switch. Like making it equal the highest spell that can be cast (like if it's 4 the spells 1 through 4 can be cast).


@ I Am Herenow

Thanks.

Yeah it's just a variable name it can be "Zarfenthootal" and it'll still work. "Picked" just seemed more natural and represents if the random number has already been "Picked" before ( 0=Not Picked 1=Picked Already ).

Switches are used when you want to check things. You turn the switch off then do the check. In this case the switch is only turned on when it sees the random number picked is the same as a number in the array. After the check you look to see if the switch is still off (or on... however you want to do it). In this case if the switch is still off then that means the random number picked is not in the array. If the switch is on it doesn't exit the do/loop so it loops again to pick another random number, turn the switch off, check the numbers, then look at the switch again.

To help you understand easier I made something I've been wanting to make for people that want to make tutorials. I did add remarks all over but you don't really have to know what this code is doing. Just run it. It will show you the code on the left side and the current line thats running in code will show up colored red. On the right side is the array list and shows the variables for the random number and "Picked". It will show the current element being worked on (with "Working>") and show the current element being checked (with "<Checking" when it's in the function).

It'll run slow so you'll have plenty of time to see exactly what the code is doing and what the results of it are. Also I reduced the random number to increase the probability that a random number picked is a number already in the array.

This works in both Classic and Pro:


Quote: "Also, what's the command that returns the largest variable from a set group/number of them? Thanks!"


You make a variable to store the current highest number and a for/next loop to look at each number in the array. When a number in the array is higher than the current highest number make the variable equal the highest number. If you want to find it by simply knowing which element in the array is higher you make another variable to represent that number.

I Am Herenow
17
Years of Service
User Offline
Joined: 23rd Dec 2006
Location:
Posted: 29th Dec 2006 10:23
Whoa, cheers, man!

For the 2nd thing I thought there was just a command like MAX() or something but I'm probably thinking of some other thing - I'll just use your example (anything that works lol).
I Am Herenow
17
Years of Service
User Offline
Joined: 23rd Dec 2006
Location:
Posted: 29th Dec 2006 13:16 Edited at: 29th Dec 2006 14:17
OK, now I have a program which gives me random (albeit valid) lottery tickets. However, that was, I'm afraid, only the first stage of my plan: this program is, if you will, a first draft. Here is how it looks at the moment:



I've put the numbers into a nice table, but that's about it for the changes.

Anyway, I read on a website about this group of mathematicians from some university who won the lottery mainly because they used a method for choosing their numbers which greatly increased their chances. They put all the numbers from 1 to 49 on separate pieces of paper and put those into a hat or bowl or something (let's just say they used bowls - it doesn't matter), and took turns to choose one (there were 17 mathematicians). They would pick a number, take it out of the bowl and write it down, and then put the number into a second bowl, getting a ticket after every 6 numbers chosen. This they did until they got 8 tickets (6 * 8 = 48 = 49 - 1) and one number left in the bowl. They then got this number and used it to start their next ticket, and then picked the following numbers out of the second bowl, and so on, until they had 17 tickets.

This admittedly absurd method improved their chances of winning because they ensured an even spread of numbers across their tickets by doing what they did. Although the law of probability should give the tickets an even spread anyway, some were bound to end up with numbers only in the 30s, say, if a standard lucky dip (which is, in effect, what my current program gives me) was done, and it is less likely to be a prizewinning ticket than one with an even spread of numbers. And by doing what they did, these mathematicians forced their tickets to have evenly-spread numbers, rather than hoping they did.

Now, despite this method "substantially increasing the chances of winning" (according to the National Lottery), I can't be bothered to do all that (and, frankly, I'm surprised that mathematicians couldn't come up with a better strategy, and suspect that there is more to this story than they reported...). Still, no matter what the mathematicians actually did, I don't want to prat around with pieces of paper, but instead want to make a program do it for me.

I was thinking of first making an array of the numbers 1-49:

Next, what I need to do (and this is the bit I need help with) is instead of making each number in each ticket a random number - RND (48)+1 - I need to make it a random number out of the array - RND (AllNumber)? And each time a number (call it AllNumber(b)) is picked, I need to stop it from being picked until all the numbers in AllNumber have been picked once, and then I reset AllNumber. Can I UNDIM just one cell (e.g. AllNumber(b))? And then check if AllNumber is empty (again, how do I do this?) and if it is, just DIM AllNumber(49)... again. Or should I use a completely different approach? Maybe I just need another switch like in Grog's example, but then I'll have a switch within a switch (the original that checks the tickets are actually valid, and another, all-powerful one evenly distributing the numbers 1 to 49 amongst the tickets).

Please help!
I Am Herenow
17
Years of Service
User Offline
Joined: 23rd Dec 2006
Location:
Posted: 30th Dec 2006 16:04
By the way, the original story is here, if you are interested:

http://www.lottery.co.uk/news/winning-formula.htm
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 31st Dec 2006 05:17 Edited at: 31st Dec 2006 05:22
Quote: "Whoa, cheers, man!"


Thanks.

Well you can do it exactly the way they did it by dimensionalizing an array to hold the tickets and the array of 49 numbers like you posted but use that as 49 different switches to represent each number. That way it's faster than doing a for/next loop that checks all numbers each loop (although you won't see a difference in speed). All you do is when a number is picked (say 42) you make "AllNumber(42)=1". So if 42 is picked again it'll quickly pick another number because "AllNumber(42)" is not at zero.

Now I'm probably doing too much for you so if you want to try it yourself first don't open this code snip till you've tried it yourself.



The method seems ok but most lottery programs don't do this.
A more accurate way to do it is to use the "AllNumber" array and increase the number each time the same number is picked... then after so many picks (say from 50000 random number picks) you look at the array to see which numbers were picked the most. The only difference is most lottery programs take a list of all the numbers in the previous picks (uploaded from the states lottery pages) and use that to determine which numbers have a higher percentage of being picked.

Hey if you use my program to win anything let me know... I don't want a cut of the profits... I'd just like to know if it was successful.
I Am Herenow
17
Years of Service
User Offline
Joined: 23rd Dec 2006
Location:
Posted: 6th Jan 2007 13:00 Edited at: 6th Jan 2007 13:17
Nice one!

I've edited it a bit (basically made it look nice lol) and now it's up and running - and I'll inform you of any further developments .

Just two more bits of theory I want you to explain to me (after this I SWEAR I'll leave you alone!)

1) a) When I change DIM Ticket (24, 5) to, say, (30, 5) (and change "IF CTicket = 25" to "IF CTicket = 31" and change "FOR t = 1 TO 24" to "FOR t = 1 TO 30") the program keeps looping - I don't press any keys, but straight after it gives me the tickets I want, it repeats the process, not giving me a chance to write them down. Have I missed a bit of code that needs editing?

b) I tried to add a thing where people could choose how many tickets they wanted with an "INPUT q" line, and changed "DIM Ticket (24,5)" to "(q, 5)" - nothing worked when I did that, so is having variables in array parameters not allowed in DBP?

2) I understood all of your program apart from 1 bit - the CTicket, CNumber etc. bit. Could you just explain what adding "C" does?

Thanks!
TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 6th Jan 2007 14:13
If you look at the top of the Newcomers board there are a number of posts highlighted with green arrows and stars. These are 'stickies' - posts which are stuck in place by mods because they contain important or useful information.

Your questions are really so basic that it's clear that you don't yet have a grasp of the fundamentals of programming - like variables.

This is like trying to learn to write when you don't know the alphabet...

This isn't having a go at you, we were all exactly the same when we started programming (in my case probably a long time before you were even born)!

So, you need to spend an hour or two reading some of the tutorials that have been written especially for people like you who have just started programming.

For example, I have a series of four tutorials for beginners covering all the basics - including variables.

Many others have done some good tutorials too, but I have deliberately gone out of my way to start my primer series at the lowest possible level and assume that you have no programming knowledge whatsoever.

Once you understand the fundamentals, the rest you will find a lot easier to understand...

TDK_Man

Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 7th Jan 2007 01:12
Thanks

You can't stop it at 30 tickets. It picks 49 numbers so full tickets go in increments of 8... the number of tickets to pick is 8, 16, 24, 32, 40, 48 and so on. I made it stop at 24 because it shows all of them on the screen at the same time (if I didn't skip element zero 32 would fit on the screen at one time).

What's probably happening is when you add the variable to allow people to pick how many tickets they want... the program needs more room because it has to have enough space for 8, 16, 24, 32, 40, 48 and so on full tickets. If the user picks something less than those numbers+1 (the +1 is needed because of the extra number after picking the 8 full tickets) it will produce an error... array out of bounds.

The "C" in both those variables represents "Current". It doesn't matter what their names are... their just variables.

Login to post a reply

Server time is: 2024-09-25 15:21:28
Your offset time is: 2024-09-25 15:21:28