Birthday problem: Difference between revisions

m
→‎version 1: added/changed comments and whitespace, used a template for the output section. optimized parts of the code.
m (added whitespace to the task's preamble, added text description to the (;See also) link.)
m (→‎version 1: added/changed comments and whitespace, used a template for the output section. optimized parts of the code.)
Line 1,265:
=={{header|REXX}}==
===version 1===
The method used is to find the average number of people to share a common birthday,   and then use the   '''floor'''   of that
<br>that value &nbsp; (less the group size) &nbsp; as a starting point to find a new group size with an expected size that exceeds
<br>50% common&nbsp; duplicate birthdays of the required size.
<lang rexx>/*REXX programpgm examines the birthday problem via random number# simulation. (with specifable parms)*/
parse arg grpsdups samp seed . /*get optional arguments from the CL. */
if grpsdups=='' | grpsdups=='",'" then grpsdups=5 10 /*Not specified? Then use the default.*/
if samp=='' | samp=='",'" then samp=100000 10000 /* " " " " " " */
if datatype(seed, 'W') then call random ,,seed /*RANDOM seed given for repeatability ?*/
diy =365 /*or: diy=365.25 */ /*the number of Days In a Year. */
diyM=diy*100 /*this expands the RANDOM (BIF) range.*/
do g=2 to dups; s=0 /*perform [↓] get a rough estimate forthrough %. 2 ──► duplicate size*/
do g=2 to grps; do s=0 samp; @.=0 /*perform throughsome number of trials. 2 ──► group size. */
do samp; @. do j=0 until @.day==g /*perform someuntil numberG of trialsdup. birthdays found.*/
do j=1 until @.day==g day=random(1, diyM) % 100 /*perform untilexpand Grange dup.RANDOM birthdaysnumber found.generation*/
@.day=random(1,diyM)@.day %+ 1 100 /*expandrecord range RANDOMthe number generationof common birthdays*/
@.day=@.day+1 end /*j*/ /*record [↓] adjust for the number ofDO common birthdaysloop index.*/
end s=s + /*j*/ /*add [↓]number of adjustbirthday forhits theto sum. DO loop index.*/
s=s+j-1 end /*samp*/ /* [↓] % /*add1 number of birthdayrounds hitsdown tothe sumdivision. */
end start.g= s/samp % 1 - g /*sampdefine where the try─outs start. */
end /*try;*/ /*g*/ /* [] bumpget thea counterrough estimate for Bday hits%. */
 
say right('sample size is ' samp, 40); say /*display this run's sample size. */
start.g=s/samp%1-g /*define where the try-outs start. */
say ' end required /*g*/ trial % with required'
say ' duplicates size common birthdays'
say
say ' ──────────── ─────── ──────────────────'
say right('sample size is ' samp,40); say /*display this run's sample size. */
say ' do g=2 to dups required group % with required' /*perform through 2 ──► duplicate size*/
say ' do try=start.g until s/samp>=.5; commons=0 /* " size try─outs until average common birthdays'50%.*/
say ' do samp; ──────── ───── ────────────────' @.=0 /* " some number of trials. */
do try; day=random(1, diyM) % 100 /* [↓] where" the try-outs until happenG dup. birthdays found.*/
do g=2 to grps @.day=@.day + 1 /*performrecord throughthe number 2 ──► group size. of common birthdays*/
do try if @.day==start.g; then do; s=0s+1; leave; end /*found enough G (birthday) hits ? /*perform try-outs until average > 50%.*/
end /*samptry;*/
do samp; @.=0 /*perform some number of trials. */
end /*try=start.gsamp*/
do try /*perform until G dup. birthdays found.*/
end day/*try=random(1,diyM)start.g*/ % 100 /*expand range[↑] where the try─outs happen. RANDOM number generation*/
say right(g, 15) right(try, 15) center( format( s / samp * 100, ,5 4)'%', 30)
@.day=@.day+1 /*record the number of common birthdays*/
end if @.day\==/*g*/ then iterate /*notstick enougha Gfork (birthday)in hitsit, found ?we're all done. */</lang>
'''{{out|output'''|text= &nbsp; when using the inputdefault ofinputs: &nbsp; <tt> 10 </tt>}}
s=s+1 /*another common birthday found. */
leave /* ··· and stop looking for more. */
end /*try;*/ /* [↓] bump the counter for Bday hits.*/
end /*samp*/
if s/samp>.5 then leave /*if the average is > 50%, then stop. */
end /*try=start.g*/
 
say right(g, 15) right(try, 15) center( format(s/samp*100,,5)'%', 30)
end /*g*/ /*stick a fork in it, we're all done. */</lang>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 10 </tt>
<pre>
sample size is 10000010000
 
required group trial % with required
common duplicates size common birthdays
──────────── ─────── ──────────────────
──────── ───── ────────────────
2 23 50.709002300%
3 8887 5150.232002400%
4 187 50.151003800%
5 314312 50.778000100%
6 460458 50.006005200%
7 623622 50.648003900%
8 798 50.007001700%
9 985984 50.134005700%
10 11811182 5051.222004000%
</pre>