Showing posts with label math. Show all posts
Showing posts with label math. Show all posts
Monday, August 27, 2012
Petit Computer Journal #4
Petit Computer Journal #4
Knowing that computer is composed of numbers makes it easy to understand. That does not make it easy to do, you see, just to understand. The point is: The computer does what you tell it to do, not what you think you tell the computer to do. Faulty language implementation is an exception to that rule, and even then, you need to figure out a way around the problem.
Computer programming can be fun, but it can be very frustrating, indeed. The question is, what will you do to make computer programming fun? If the answer is to make games, then you're probably off to the wrong start. If the answer is to make games that people want to play, then you're probably off to the right start.
You see, computer programming is all about problem solving. What problem are you trying to solve? Without a clear goal, you probably will just do things randomly. Maybe you discover something, or maybe not. With a clear goal, however, you can work on the steps to achieving that goal. Then, when you finally solve that problem and arrive at your goal, your satisfaction level is quite high. That, my friend, is the fun of computer programming.
It's like winning a game, solving a puzzle, guessing a riddle. Before you did it, you don't know. After you did it, you know. That's fun.
Speaking of goals, you are still a beginner, so let's keep things simple for now. Let's start by figuring out how to tell a computer to do something simple.
Inkey$, input, linput
Let's get the keyboard input out of the way real quick. What is the difference between INKEY$, INPUT, AND LINPUT? Write a small program to check it out!
'Keyboard input source code example
@MAINLOOP
VSYNC 1: A$=INKEY$
IF A$!="" THEN B$=A$
LOCATE 0,0:?"A$=";A$;" "
LOCATE 0,2:?"B$=";B$;" "
GOTO @MAINLOOP
There are 3 things you have to see here:
VSYNC 1: This synchronized the system 1/60 second. Try it with different values and see what happens!
A$=INKEY$: This gets the keyboard value and assign it to A$
IF A$!="" THEN B$=A$: Since the value disappear at the next iteration, and we want to keep the old value, we assign the value of A$ to B$, but only if there is something to copy.
And that's it! If you are writing a computer game that cannot wait for user input, then INKEY$ is the way to go. I understand that it isn't the easiest to use, but it's there if you want it.
What's the difference between INPUT AND LINPUT? Try them out and see!
@MAINLOOP
VSYNC 1: INPUT "X,Y",X,Y
?"X=";X:?"Y=";Y
?:?:'DOUBLE BLANK LINES
VSYNC 1: LINPUT "DATA:";A$
?"A$=";A$
WAIT 300
GOTO @MAINLOOP
INPUT takes several inputs and assign them to different variables. This is very useful for inputting numeric values. Just type them out and separate them with comma. LINPUT takes in a string, commas included. Just one string.
Can you take string and numbers using INPUT? Try it and see!
@MAINLOOP
VSYNC 1: INPUT "X$,Y",X$,Y '<==change here
?"X=";X$:?"Y=";Y '<==Change here
?:?:'DOUBLE BLANK LINES
VSYNC 1: LINPUT "DATA:";A$
?"A$=";A$
WAIT 300
GOTO @MAINLOOP
Yes, you can! If the program doesn't understand your input, it will ask you to re-enter the data "?Redo from start". In which case, you re-enter the data, hopefully without mistake this time!
Touch Screen TCHX,TCHY,TCHST,TCHTIME PNLTYPE
Buttons and keyboard are nice, but we have something there that is just begging to be used: Touchscreen! I know I'm bucking the convention here, since most people are satisfied writing their beginner's program using INPUT or BUTTON(), but I really want to use the touch screen. Fortunately, it's as easy to use as INKEY$.
It's just some variables, and you even use it like you do INKEY$. The difference is that there is more than one, and you use numeric variables. Let's do it. It is helpful if we disable the on-screen keyboard. We do it via PNLTYPE command. Simply set it to "OFF".
'Touchscreen input source code example
PNLTYPE "OFF"
CLS
@MAINLOOP
VSYNC 1
X=TCHX:Y=TCHY:S=TCHST:T=TCHTIME
LOCATE 0,0:?"X=";X;" "
LOCATE 0,2:?"Y=";Y;" "
LOCATE 0,4:?"S=";S;" "
LOCATE 0,6:?"T=";T;" "
GOTO @MAINLOOP
Hit the Select button to stop the program. Yup. Just like INKEY$. The difference is, X and Y values are not reset to zero when the stylus is off the screen even though the status (TCHST) and timer (TCHTIME) are reset.
Math - Arithmetic
Alright, I need you to confess: Who among you have not yet finished 1st Grade? You know, the school grade right after Kindergarten? Whaat? You don't know Arithmetic? Oh, dear. This is bad. I think maybe you need to learn how before we can continue.
Haha, joking aside, you do need to know Arithmetic. Some Trigonometry, too. How about Algebra? Yes, you do need to know how to manipulate variables. Fortunately, if you know Show-and-Tell, you may be alright. Here's a little something that's useful to know: Mapping function.
map() is something that is built-in in Processing computer programming language. It's a good programming language. Check out www.processing.org for details.
In the meantime, let's implement it in BASIC. The idea is, if you have a number that is between two numbers, given another two numbers, what will be the number that has the same ratio as the first? In mathematical format, assuming X is the first number, and Y is the second, we have
X1-X2-X3 X2 is between X1 & X3
Y1-Y2-Y3 Y2 is between Y1 & Y3
Since the ratio is the same, we have
(X2-X1)/(X3-X1)=(Y2-Y1)/(Y3-Y1)
Solve for Y2:
(Y2-Y1)/(Y3-Y1)=(X2-X1)/(X3-X1)
(Y2-Y1)=((X2-X1)/(X3-X1))*/(Y3-Y1)
Y2=(((X2-X1)/(X3-X1))*/(Y3-Y1))+Y1
And that's all there is to it! Work it out on paper if you're having trouble. I find it helpful to draw triangles to visualize the problem.
A Math Explorer sample program
We want to feature a lot of math here, so we're going to just do it all in one program. The program will features different modes, and take inputs from touch screen, normalized to 0-5 for both X and Y. Furthermore, if the stylus is on top-left corner, we'll change the mode.
0: Simple Arithmetic
1: Math functions
2: Logical Math
3: Exponent
Here goes:
Part 1: Touch screen input. No problem there. Just copy it from the sample program above.
'Math Explorer
PNLTYPE "OFF"
CLS
@MAINLOOP
VSYNC 1
X=TCHX:Y=TCHY:S=TCHST:T=TCHTIME
Part 2: Display X,Y, Mode, and Cycle Mode. If you remember our COUNTER program example? Yup, just like that!
IF (X<32 AND Y<24 AND T==1) THEN MODE=MODE+1
MODE=MODE%4:'MODE=0-3
LOCATE 0,0
?"X=";X;" Y=";Y;" MODE=";MODE
Part 3: Normalize X and Y. We already have the mathematical formula for this. Just implement that using GOSUB.
X1=0:X2=X:X3=255:Y1=0:Y3=5:GOSUB @MAP:XP=Y2:'CALCULATES XP=Y2
X1=0:X2=Y:X3=191:Y1=0:Y3=5:GOSUB @MAP:YP=Y2:'CALCULATES YP=Y2
?"XP=";XP;" YP=";YP;" "
GOTO @MAINLOOP
@MAP
Y2=(((X2-X1)/(X3-X1))*(Y3-Y1))+Y1
RETURN
And that's the beginning. We'll continue with Part 4 next, but first, run this program and see that we have normalized XP and YP, and that the MODE cycles 0-3 satisfactorily.
Change the program slightly to this before continuing. Yes, I expect you to be able to read! I know it's hard in the beginning, but please persevere.
'Math Explorer
PNLTYPE "OFF"
CLS
@MAINLOOP
VSYNC 1:IF MODE!=2 THEN CLS
X=TCHX:Y=TCHY:S=TCHST:T=TCHTIME
IF (X<32 AND Y<24 AND T==1) THEN MODE=MODE+1
MODE=MODE%4:'MODE=0-3
LOCATE 0,0
?"X=";X;" Y=";Y;" MODE=";MODE;" "
X1=0:X2=X:X3=255:Y1=0:Y3=5:GOSUB @MAP:XP=Y2:'CALCULATES XP=Y2
X1=0:X2=Y:X3=191:Y1=0:Y3=5:GOSUB @MAP:YP=Y2:'CALCULATES YP=Y2
?"XP=";XP;" YP=";YP;" "
GOTO @MAINLOOP
@MAP
Y2=(((X2-X1)/(X3-X1))*(Y3-Y1))+Y1
RETURN
And that's the program. Now, here is some snippets of code. I trust that you know where to put this.
?:?
ON MODE GOTO @ARIT, @FUNC,@LOGI, @EXPO
GOTO @MAINLOOP:'INVALID CHOICE
@ARIT
?"X+Y=";(X+Y)
?"X-Y=";(X-Y)
?"X*Y=";(X*Y)
IF Y!=0 THEN ?"X/Y=";(X/Y)
IF Y!=0 THEN ?"X%Y=";(X%Y)
IF YP!=0 THEN ?"X%YP=";(X%YP)
GOTO @ENDLOOP
@FUNC
?"FLOOR(XP)=";FLOOR(XP)
?"RND(X)=";RND(X)
?"RND(XP)+20=";RND(XP)+20
?"ABS(X-128)=";ABS(X-128)
?"SGN(X-128)=";SGN(X-128)
?"SWAP ":SWAP XP,YP
?"XP=";XP;" YP=";YP
IF S==1 THEN WAIT 30
GOTO @ENDLOOP
@LOGI
LOCATE 0,4
?"X= ";:V=X:GOSUB @BIN
?"Y= ";:V=Y:GOSUB @BIN
?"AND ";:V=(X AND Y):GOSUB @BIN
?"OR ";:V=(X OR Y):GOSUB @BIN
?"XOR ";:V=(X XOR Y):GOSUB @BIN
?
?"X= ";:V=X:GOSUB @BIN
?"NOT ";:v=NOT(X):GOSUB @BIN
?"! ";:v=!X:GOSUB @BIN
GOTO @ENDLOOP
@EXPO
?"SQR(X)=";SQR(X)
?"EXP(XP)=";EXP(XP)
IF X!=0 THEN ?"LOG(X)=";LOG(X)
?"POW(2,XP)=";POW(2,XP)
?"POW(3,XP)=";POW(3,XP)
@ENDLOOP
GOTO @MAINLOOP
'SPLIT THIS TO THE END
@BIN
FOR I=0 TO 7
P=POW(2,I)
IF (P AND V) THEN ?"1"; ELSE ?"0";
NEXT
RETURN
One more round of Math and we'll be done! It's all about Trigonometry. We'll save it for later until after we learn Graphics!
Wednesday, July 18, 2012
How much is it, exactly?
How much is it, exactly?
There is a news report saying that millions of kids simply don't find school very challenging. From USA Today, July 10, 2012, it has been found that among other things, 37% of fourth-graders say their math is "often" or "always" too easy. This has been cited as proof that kids need to pushed harder to reach their maximum potential.
To which, I say, if 37% is too much, how much is desirable? The article didn't say.
There is a common pattern that basically ignores the other side. Blame it on TV, on politicians, on schools, or whatever. The fact is, there aren't that many people who says, "If that isn't it, then what is?"
I lost count of the number of arguments who says "I am sooo big. You are sooo small!"
And my response is always, "How much exactly? How do you know? Do you have any data that backs your assertion?" I am sad to say that 9 out of 10 does not have any answer whatsoever. I'm not saying their answer is faulty, but that they have no answer.
This, among other things, indicates to me the lack of empathy, and courtesy, and common sense. Just how hard is it to ask the question, and answer it?
Very hard, it turns out.
There is a kickstarter project by DreamQuest Games called "Alpha Colony". It tries to update Dan Bunten's M.U.L.E. game to current high standard. Admirable goal. It asks for $500,000. Ouch! A lot of people, rightly so, thinks that is too high. DreamQuest's response? "Half the money goes toward expenses, including licensing and advertising. The other half goes toward multi-platform development."
That kind of distribution isn't strange. It is pretty standard. I was thinking "They need to learn how to develop for multi-platform more efficiently." Followed by, "What do they need advertising budget for? Kickstarter provides all the funds!" Followed by, "How much exactly do they need to develop multi-platform? How much cost is advertising, and how much is licensing?"
See how it clarifies the picture? When pushed, DreamQuest said that they are willing to explain how their costs structure is calculated. I think that's the wrong response. The right response, IMO, would be "If $500,000 is too much for top-quality, multi-platform, licensed game, then how much do you think is reasonable?"
See how simple it is? Once you arrived at reasonable figure, then you can do graduated features. For example: $100,000=good gameplay on PC. $200,000=multi platform. $350,000=Premium art. $500,000=Animation, and voice acting. That's no problem at all. Licensing, can be arranged as percentage (you may need to give more percentage if you're not giving them advance, but that's part of doing business.)
As I type this, Alpha Colony gathers $100,000 funding, failing short $400,000. Imagine had they asked that simple question. They'd be successful by now!
Another comparison is Kickstarter project "OUYA". $99 open platform networked console. The design is by Ives Behar. It is beautiful! As I'm typing this, the project gathers funding at a rate close to 1 million dollars PER DAY!
Quite a big difference, wouldn't you say?
I wish OUYA developers much success. The world can use an open development console like that. Then again, I can think of an already existing solution that reads like what OUYA is promising: Raspberry Pi.
This is a Linux-based computing (Android is planned for the future) that costs $35! Add $30 controllers and you get $34 dollars for Wifi, bluetooth, and extra storage.
Plus, it runs Linux, which is very open, programmable, and easy to develop. Support is easy to come by. What's the difference between OUYA and Raspberry Pi? No difference that I can see!
Manufacturing such technology is very difficult, indeed. Raspberry designers kept making compromises in order to keep the price down. Can OUYA developer says the same thing? Doubt it.
I hope I'm wrong with this and that OUYA will delivers above expectation. However, my guess is that OUYA will end up buying Raspberry Pi by the bulk, cheap plastic stamp controllers, and put the console in a very pretty cardboard box designed by Ives Behar. It will also run Linux with Android feature promised as a future upgrade.
See how specific info can tell you what the most likely path is? The more specific data that you can get, the better off you are.
Back to the original question: If 37% "too easy" isn't right, then what is? The article has a slant that they want less people to find it too easy. Personally, I want more people to find it too easy. 50% too easy, 50% too hard, is a good balance to get. Math is cumulative, and a good foundation now is priceless in the future.
Labels:
alpha colony,
Behar,
console,
development,
dreamquest,
exactly,
how much,
Ives,
kickstarter,
math,
open,
OUYA,
Pi,
Raspberry,
too easy,
too hard
Tuesday, June 5, 2012
Poker Hand Probabilities using nCr
'Poker probabilities - ZFS003-0
'Harry Hardjono
'May 2012
'
' Verified: Royal Flush, Flush
'I wonder why the probabilities don't match?
'
'
'All cards possibilities
S=52*51*50*49*48
TextWindow.WriteLine("All Card Possibilities: "+S)
'Royal Flush
P=20*4*3*2*1
TextWindow.WriteLine("Royal Flush: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'Straight Flush
P=32*4*3*2*1
TextWindow.WriteLine("Straight Flush: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'Four of a Kind
P=52*3*2*1*48
TextWindow.WriteLine("Four of a Kind: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'Full House
P=52*3*2*48*3
TextWindow.WriteLine("Full House: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'Flush
P=52*12*11*10*9
TextWindow.WriteLine("Flush: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'Straight
P=52*16*12*8*4
TextWindow.WriteLine("Straight: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'Three of a Kind
P=52*3*2*48*44
TextWindow.WriteLine("Three of a Kind: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'Two Pair
P=52*3*48*3*44
TextWindow.WriteLine("Two Pair: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'One Pair
P=52*3*48*44*42
TextWindow.WriteLine("One Pair: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'High Card
P=52*48*44*42*38
TextWindow.WriteLine("High Card: 1 in "+Math.Round(S/P)+" "+(P*100/S))
TextWindow.WriteLine(" ")
TextWindow.WriteLine("These combinations are verified on Wiki")
'Combination
N=52
R=5
nCr()
S=C
TextWindow.WriteLine("Combination: "+C+" ")
'Royal Flush
N=4
R=1
nCr()
TextWindow.WriteLine("Royal Flush: 1 in "+Math.Round(S/C)+" "+(C*100/S))
'Straight Flush
ArrN="1=10;2=4;3=4;"
ArrR="1=1;2=1;3=1;"
MultinCr()
P=(ArrC[1]*ArrC[2])-ArrC[3]
TextWindow.WriteLine("Straight Flush: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'Four of a Kind
ArrN="1=13;2=12;3=4;"
ArrR="1=1;2=1;3=1;"
MultinCr()
P=(ArrC[1]*ArrC[2]*ArrC[3])
TextWindow.WriteLine("Four of a Kind: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'Full House
ArrN="1=13;2=4;3=12;4=4;"
ArrR="1=1;2=3;3=1;4=2;"
MultinCr()
P=(ArrC[1]*ArrC[2]*ArrC[3]*ArrC[4])
TextWindow.WriteLine("Full House: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'Flush
ArrN="1=13;2=4;3=10;4=4;"
ArrR="1=5;2=1;3=1;4=1;"
MultinCr()
P=(ArrC[1]*ArrC[2])-(ArrC[3]*ArrC[4])
TextWindow.WriteLine("Flush: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'Straight
ArrN="1=10;2=4;3=10;4=4;"
ArrR="1=1;2=1;3=1;4=1;"
MultinCr()
P=(ArrC[1]*ArrC[2]*ArrC[2]*ArrC[2]*ArrC[2]*ArrC[2])-(ArrC[3]*ArrC[4])
TextWindow.WriteLine("Straight: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'Three of a Kind
ArrN="1=13;2=4;3=12;4=4;"
ArrR="1=1;2=3;3=2;4=1;"
MultinCr()
P=(ArrC[1]*ArrC[2]*ArrC[3]*ArrC[4]*ArrC[4])
TextWindow.WriteLine("Three of a Kind: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'Two Pair
ArrN="1=13;2=4;3=11;4=4;"
ArrR="1=2;2=2;3=1;4=1;"
MultinCr()
P=(ArrC[1]*ArrC[2]*ArrC[2]*ArrC[3]*ArrC[4])
TextWindow.WriteLine("Two Pair: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'One Pair
ArrN="1=13;2=4;3=12;4=4;"
ArrR="1=1;2=2;3=3;4=1;"
MultinCr()
P=(ArrC[1]*ArrC[2]*ArrC[3]*ArrC[4]*ArrC[4]*ArrC[4])
TextWindow.WriteLine("One Pair: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'High Card
ArrN="1=13;2=10;3=4;4=4;"
ArrR="1=5;2=1;3=1;4=1;"
MultinCr()
P=(ArrC[1]-ArrC[2])*(ArrC[3]*ArrC[3]*ArrC[3]*ArrC[3]*ArrC[3]-ArrC[4])
TextWindow.WriteLine("High Card: 1 in "+Math.Round(S/P)+" "+(P*100/S))
Sub MultinCr
For m=1 To Array.GetItemCount(ArrN)
N=ArrN[m]
R=ArrR[m]
nCr()
ArrC[m]=C
EndFor
endsub
'nCr = (n!)/(r!*(n-r)!)
Sub nCr
'N (input)
'R (input)
C=1 '(output)
For i=Math.Max(R,(N-R))+1 To N
C=C*i
EndFor
For i=1 To Math.Min(R,(N-R))
C=C/i
EndFor
EndSub
TextWindow.WriteLine(" ")
TextWindow.WriteLine("These combinations are verified on Wiki")
'Combination
N=52
R=5
nCr2()
S=C
TextWindow.WriteLine("Combination: "+C+" ")
'Royal Flush
N=4
R=1
nCr2()
TextWindow.WriteLine("Royal Flush: 1 in "+Math.Round(S/C)+" "+(C*100/S))
'Straight Flush
ArrN="1=10;2=4;3=4;"
ArrR="1=1;2=1;3=1;"
MultinCr2()
P=(ArrC[1]*ArrC[2])-ArrC[3]
TextWindow.WriteLine("Straight Flush: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'Four of a Kind
ArrN="1=13;2=12;3=4;"
ArrR="1=1;2=1;3=1;"
MultinCr2()
P=(ArrC[1]*ArrC[2]*ArrC[3])
TextWindow.WriteLine("Four of a Kind: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'Full House
ArrN="1=13;2=4;3=12;4=4;"
ArrR="1=1;2=3;3=1;4=2;"
MultinCr2()
P=(ArrC[1]*ArrC[2]*ArrC[3]*ArrC[4])
TextWindow.WriteLine("Full House: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'Flush
ArrN="1=13;2=4;3=10;4=4;"
ArrR="1=5;2=1;3=1;4=1;"
MultinCr2()
P=(ArrC[1]*ArrC[2])-(ArrC[3]*ArrC[4])
TextWindow.WriteLine("Flush: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'Straight
ArrN="1=10;2=4;3=10;4=4;"
ArrR="1=1;2=1;3=1;4=1;"
MultinCr2()
P=(ArrC[1]*ArrC[2]*ArrC[2]*ArrC[2]*ArrC[2]*ArrC[2])-(ArrC[3]*ArrC[4])
TextWindow.WriteLine("Straight: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'Three of a Kind
ArrN="1=13;2=4;3=12;4=4;"
ArrR="1=1;2=3;3=2;4=1;"
MultinCr2()
P=(ArrC[1]*ArrC[2]*ArrC[3]*ArrC[4]*ArrC[4])
TextWindow.WriteLine("Three of a Kind: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'Two Pair
ArrN="1=13;2=4;3=11;4=4;"
ArrR="1=2;2=2;3=1;4=1;"
MultinCr2()
P=(ArrC[1]*ArrC[2]*ArrC[2]*ArrC[3]*ArrC[4])
TextWindow.WriteLine("Two Pair: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'One Pair
ArrN="1=13;2=4;3=12;4=4;"
ArrR="1=1;2=2;3=3;4=1;"
MultinCr2()
P=(ArrC[1]*ArrC[2]*ArrC[3]*ArrC[4]*ArrC[4]*ArrC[4])
TextWindow.WriteLine("One Pair: 1 in "+Math.Round(S/P)+" "+(P*100/S))
'High Card
ArrN="1=13;2=10;3=4;4=4;"
ArrR="1=5;2=1;3=1;4=1;"
MultinCr2()
P=(ArrC[1]-ArrC[2])*(ArrC[3]*ArrC[3]*ArrC[3]*ArrC[3]*ArrC[3]-ArrC[4])
TextWindow.WriteLine("High Card: 1 in "+Math.Round(S/P)+" "+(P*100/S))
Sub MultinCr2
For m=1 To Array.GetItemCount(ArrN)
N=ArrN[m]
R=ArrR[m]
nCr2()
ArrC[m]=C
EndFor
endsub
'nCr = (n!)/(r!*(n-r)!)
Sub nCr2
'N (input)
'R (input)
C=N '(output)
For i=2 To Math.Min(R,(N-R))
C=C*(N-(i-1))/i
EndFor
EndSub
Subscribe to:
Posts (Atom)
