The Museum of HP Calculators

HP Forum Archive 17

 Coin flip programMessage #1 Posted by Vincze on 28 Aug 2007, 8:45 a.m. Last night, I get funny idea to make program to simulate coin flip on 35s. This because wife and son say I not flipping coin right to see who win something. Anyhow, I figure if I make program, it should be fair and not influence by human and odds should be 50/50. I also tell my son that on true false test, there is a 50/50 chance on getting answer correct, but there is a 90% chance you will pick the wrong answer (that was joke in Hungary). ```F001 LBL F F002 0.5 F003 RANDOM F004 X>Y? F005 GTO F008 F006 EQN HEADS F007 GTO F001 // I DO THIS SO IT KEEP RECYCLING IF NEEDED F008 EQN TAILS F009 GTO F001 F010 RTN // NOT SURE IF THIS NEEDED, BUT IT HERE JUST IN CASE ``` I put RTN at F010, even though program should never get there. I just think it better programing practice to always end program formally. I would be interested to hear what others say about that though.

 RPL versionMessage #2 Posted by Vincze on 28 Aug 2007, 2:53 p.m.,in response to message #1 by Vincze My friends, I thought I try this simple program and convert to RPL, but I have hard time with. Here is what I have. ```<< IF RAND > .5 THEN 'HEADS' ELSE 'TAILS' END >> ``` When I run it always head. Do I need store the randum number to a variable? Also, it display this is stack `'HEADS'>.5624896409... ` That seem strange if you ask me.

 Re: RPL versionMessage #3 Posted by Raymond Del Tondo on 28 Aug 2007, 3:16 p.m.,in response to message #2 by Vincze Hello, please don't forget it's RPL;-) The syntax of your IF clause should be as follows: << IF RAND .5 > THEN 'HEADS' ELSE 'TAILS' END >> HTH Raymond

 Re: RPL versionMessage #4 Posted by Vincze on 28 Aug 2007, 3:32 p.m.,in response to message #3 by Raymond Del Tondo Okay, so THEN and 'HEADS' must be on same line. I look at example I have in RPL guide and it have it differently. I will give this a try. Thank you. **EDIT** no, still always HEADS. I will look into assigning to variable first and see if that issue. **Edit 2** I see what I do wrong. I not read your message properly and see that .5 come before greater than sign. Yes, Víncze, Reverse Polish Lisp. Think reverse. Okay, now it work nicely. Thank you my friend. Edited: 28 Aug 2007, 3:37 p.m.

 Re: RPL versionMessage #5 Posted by James M. Prange (Michigan) on 28 Aug 2007, 9:25 p.m.,in response to message #4 by Vincze It would be better to return character strings than global names, so I'd use "HEADS" and "TAILS" instead of 'HEADS' and 'TAILS'. Assuming that the AUR manual is correct, RAND returns a value in the range 0<=x<1, so for true 50/50 probabilities, use something like: ```%%HP: T(3)A(R)F(.); \<< IF RAND .5 \>= THEN "HEADS" ELSE "TAILS" END \>> ``` or: ```%%HP: T(3)A(R)F(.); \<< IF RAND .5 < THEN "TAILS" ELSE "HEADS" END \>> ``` In the above, "%%HP: T(3)A(R)F(.);" is an "ASCII transfer header" that would allow you to copy and paste the source code to a file for transferring to a calculator. It tells the calculator, first off, that it is an ASCII transfer, then which translation mode (0 through 3) to use, then which angular mode (DEG, RAD, or GRAD) to use, and which of period or comma is the "fraction mark". For small programs such as these, one would likely just key in the program, but the transfer header tells just how to key it in, although the angular mode doesn't matter for these particular cases. "\<<" and "\>>" represent the UserRPL program delimiters, and "\>=" represents the "greater than or equal to" symbol. In RPL source code, a "line" is pretty much meaningless, except that a LineFeed code always end any comment. You could put everything on a single line, use one line for each "word", indent some lines, or whatever you choose. If a source code delimiter doesn't already do so, then "separators", such as spaces, ASCII control codes, semicolon, or whichever of period or comma isn't the fraction mark, tell the compiler where one "word" ends and the next begins. Such source code "formatting" is up to the programmer, and each tends to use his own particular formatting "style"; for example, with a longer program, I'd use something like: ```%%HP: T(3)A(R)F(.); \<< IF RAND .5 \>= THEN "HEADS" ELSE "TAILS" END \>> ``` Getting back to RAND, it would be possible to cheat with your program by setting the seed with the RDZ command. With any given seed, the numbers returned by a series of RAND commands is repeatable, although rather difficult to "predict" without actually trying it, thus you could "preview" the sequence of "HEADS" and "TAILS" returned after "initializing" the seed with the RDZ command. Regards,James

 Re: RPL versionMessage #6 Posted by Paul Dale on 28 Aug 2007, 9:56 p.m.,in response to message #5 by James M. Prange (Michigan) If we're going to dig in and alter the code, I'd do it this way: ``` << "HEADS" "TAILS" RAND .5 < IFTE >> ``` Smaller, faster and less easy to understand :-) - Pauli

 Re: RPL versionMessage #7 Posted by James M. Prange (Michigan) on 28 Aug 2007, 10:12 p.m.,in response to message #6 by Paul Dale Quote: If we're going to dig in and alter the code, I'd do it this way: ``` << "HEADS" "TAILS" RAND .5 < IFTE >> ``` Smaller, faster and less easy to understand :-) But to make it actually work: ```%%HP: F(.); \<< RAND .5 < "HEADS" "TAILS" IFTE \>> ``` Regards,James

 Re: RPL versionMessage #8 Posted by Paul Dale on 28 Aug 2007, 10:59 p.m.,in response to message #7 by James M. Prange (Michigan) oops :-) Teach me to not try out the code before posting. - Pauli

 Re: RPL versionMessage #9 Posted by Vincze on 29 Aug 2007, 9:00 a.m.,in response to message #6 by Paul Dale Quote: If we're going to dig in and alter the code, I'd do it this way: ``` << "HEADS" "TAILS" RAND .5 < IFTE >> ``` Smaller, faster and less easy to understand :-) - Pauli IFTE?? IF Than Else? Yes, much smaller and very not easy to understand, but yes efficient (I think).

 Re: RPL versionMessage #10 Posted by Vincze on 29 Aug 2007, 8:57 a.m.,in response to message #5 by James M. Prange (Michigan) Quote: It would be better to return character strings than global names, so I'd use "HEADS" and "TAILS" instead of 'HEADS' and 'TAILS'. Yes, I actually did that, but did not update my code online Quote: Assuming that the AUR manual is correct, RAND returns a value in the range 0<=x<1, so for true 50/50 probabilities, use something like: ```%%HP: T(3)A(R)F(.); \<< IF RAND .5 \>= THEN "HEADS" ELSE "TAILS" END \>> ``` or: ```%%HP: T(3)A(R)F(.); \<< IF RAND .5 < THEN "TAILS" ELSE "HEADS" END \>> ``` Yes, I realize this after I post. I make same change. Quote: In the above, "%%HP: T(3)A(R)F(.);" is an "ASCII transfer header" that would allow you to copy and paste the source code to a file for transferring to a calculator. It tells the calculator, first off, that it is an ASCII transfer, then which translation mode (0 through 3) to use, then which angular mode (DEG, RAD, or GRAD) to use, and which of period or comma is the "fraction mark". For small programs such as these, one would likely just key in the program, but the transfer header tells just how to key it in, although the angular mode doesn't matter for these particular cases. "\<<" and "\>>" represent the UserRPL program delimiters, and "\>=" represents the "greater than or equal to" symbol. In RPL source code, a "line" is pretty much meaningless, except that a LineFeed code always end any comment. You could put everything on a single line, use one line for each "word", indent some lines, or whatever you choose. If a source code delimiter doesn't already do so, then "separators", such as spaces, ASCII control codes, semicolon, or whichever of period or comma isn't the fraction mark, tell the compiler where one "word" ends and the next begins. Such source code "formatting" is up to the programmer, and each tends to use his own particular formatting "style"; for example, with a longer program, I'd use something like: ```%%HP: T(3)A(R)F(.); \<< IF RAND .5 \>= THEN "HEADS" ELSE "TAILS" END \>> ``` Getting back to RAND, it would be possible to cheat with your program by setting the seed with the RDZ command. With any given seed, the numbers returned by a series of RAND commands is repeatable, although rather difficult to "predict" without actually trying it, thus you could "preview" the sequence of "HEADS" and "TAILS" returned after "initializing" the seed with the RDZ command. Regards,James This all good information, and thank you my friend for sharing with me. It always goo to learn from someone who know more and I appreciate your help.

Go back to the main exhibit hall