Coin flip program



#2

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.


#3

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.


#4

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


#5

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.


#6

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


#7

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


#8

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


#9

oops :-)

Teach me to not try out the code before posting.


- Pauli

#10

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).

#11

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.


Possibly Related Threads…
Thread Author Replies Views Last Post
  HP Prime: run a program in another program Davi Ribeiro de Oliveira 6 2,738 11-11-2013, 08:28 PM
Last Post: Davi Ribeiro de Oliveira
  Flip Flap--15C battery bay flaps Matt Agajanian 3 1,402 03-04-2012, 07:17 PM
Last Post: Raymond Del Tondo
  current draw from CR2032 coin cells Eric Smith 5 1,954 11-04-2010, 12:32 AM
Last Post: hpnut
  CR2032 coin cells 20 for $5 designnut 2 1,139 04-15-2009, 09:33 PM
Last Post: Paul Dale
  Mini challenge - coin toss MikeO 33 6,836 03-23-2009, 03:54 PM
Last Post: htom trites jr
  HP-41 MCODE: Making an MCODE program call another MCODE program Geir Isene 10 2,888 01-13-2008, 05:58 AM
Last Post: Raymond Del Tondo
  42s hardcover flip-case ECL 2 1,058 11-09-2005, 11:51 AM
Last Post: Richard Garner

Forum Jump: