Not applicable
Posts: 0

# Coin Toss Problem

I am doing a coin toss simulation, simple enough (below is my code)
but I want to add one more step to it and not sure how do I go about it:
If I get Heads, I stop, but if I get a Tail, I toss again, if I get a head I stop, but again if I get a tail I toss again......I keep tossing until I get a head, then I add up all the times I get a head and all the tails.
I guess I need a macro and another while loop, but I can't seem to get it, any help would be greatly appreciated. THANKS!

DATA random;
DO i=1 to 100;
x = UNIFORM(123456);
IF x>.5 THEN coin = 'heads';
ELSE coin = 'tails';
OUTPUT;
END;
RUN;

PROC FREQ DATA=random;
table coin;
RUN;
Valued Guide
Posts: 2,191

## Re: Coin Toss Problem

for the purpose of clarity, your challenge is perhaps in need of more sample input and output data.
Before working hard to simulate something that is delivered with a SAS function, have you looked at "Using Random-Number Functions" http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a001281561.htm ?

PeterC
Not applicable
Posts: 0

## Re: Coin Toss Problem

I am using Random numbers, I made few changes but still not right, any help???

data coin;

Tailcount=0;

do k=1 to 1000; /*do it 1000 times*/

c = 0; /* this is to reset the loop if I get tail*/
do while (c = 0);

do i = 1 to 100;
x = ranuni (-3); /*generate randon number*/
IF x>.5 THEN do;
c = 1;

OUTPUT;
end;
else do;
TailCount=TailCount+1;
c=0;
end;

end;
end;

output;
file print ;
put 'Number of Tail=' TailCount;

end;
Contributor
Posts: 74

## Re: Coin Toss Problem

have you tried do while ('tail condition') loop?
Not applicable
Posts: 0

## Re: Coin Toss Problem

No, I don't even know what that is, I will look it up. Thanks
Any help would be appreciate it!
Posts: 4,738

## Re: Coin Toss Problem

Are you after something like the following?

proc format;
low - 0.5 = 'Tail'
;
run;

DATA random;
DO group=1 to 100;
toss=1;
coin = UNIFORM(123456);
do until (coin>.5);
output;
coin = UNIFORM(123456);
toss+1;
end;
END;
RUN;

PROC FREQ DATA=random;
table group*coin;
RUN;
Not applicable
Posts: 0

## Re: Coin Toss Problem

I think this is what I am looking for. Thank you so much!
I am still not good at interpreting things is SAS and I can't make out the results.

What I am trying to prove is that at the end you have the same (almost) number of tails and heads. Lets say I start with 50 coins
I will get 25 heads and 25 tails (in a perfect scenario)
out of the 25 tails I toss again
I will get 12.5 heads and 12.5 tail (lets say 12 heads and 13 Tail)
out of the 13 tail I toss again
I will get 6 heads and 7 tails ( rounded)
out of the 7 tails I toss again
I will get 3 heads and 4 tails
out of the 4 tails I toss again
I will get 2 heads and 2 tails
out of 2 tails I toss again
I get 1 head and one tails
if I count all the heads I will get
25+12+6+3+2+1=49
now lets count the tails
25+13+7+4+2+1=52
so at the end I still have almost a 1:1 ratio

is that what I should expect when I run your code?
Thanks again for all you help
Posts: 4,738

## Re: Coin Toss Problem

Yes, that's what you can expect. You just would have to analyse the generated data appropriately (a simple Proc Freq won't do).

But.... What do you want to prove? That a SAS uniform distribution works as documented?
In the end SAS also uses only a mathematical algorithm to generate the random numbers - and I assume you'll find this algorithm documented... somewhere.

So: If you want to prove something then it would be a mathematical approach proofing that the algorithm used is correct and an empirical proof showing that SAS really uses the algorithm as documented.

And because it's about random numbers with no dependencies: Just generate a lot of observations and look at the distribution – no point to analyse sub-groups. It’s always the same probability so what do you expect?
Not applicable
Posts: 0

## Re: Coin Toss Problem

Thank you Patrick!
Believe or not my degree is in Mathematics, but I find Statistics confusing!
I am not going to prove the algorithm, I just wanted a simple simulation of it.
I have been trying to learn SAS for the past 2 months and I am in a love/hate relationship; while some days I can code easily, other days I find myself stuck and not really knowing where to start. So thank you so much for your help, I really do appreciate it
PROC Star
Posts: 2,370

## Re: Coin Toss Problem

Interestingly, we don't get 50% at all, we get 75%-25%.

That's because the last draw (that stops the loop) is not saved. So each time we have to redraw (1 out of 2 times), we don't save the final head.

This might be better:

DATA random;
do group=1 to 1000;
toss=1;
do until (coin>.5);
coin = ranuni(0);
output;
toss+1;
end;
end;
run;
PROC FREQ DATA=random;
table coin;
RUN;
Discussion stats
• 9 replies
• 1282 views
• 0 likes
• 5 in conversation