Jump to content

Birthday problem: Difference between revisions

m
→‎version 1: changed whitespace and some comments; added wording to the REXX section header.
m (→‎version 1: changed whitespace and some comments; added wording to the REXX section header.)
Line 1,540:
=={{header|REXX}}==
===version 1===
The root finding method used is to find the average number of people to share a birthday,   and then use the   '''floor'''   of that
<br>of 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% &nbsp; duplicate birthdays of the required size.
 
<lang rexx>/*REXX pgm examines the birthday problem via random # simulation (with specifable parms)*/
This REXX version doesn't need a precalculated group size to find the percentage required to exceed 50%.
<lang rexx>/*REXX pgm examines the birthday problem via random # simulation (with specifablespecifiable parms)*/
parse arg dups samp seed . /*get optional arguments from the CL. */
if dups=='' | dups=="," then dups= 10 /*Not specified? Then use the default.*/
if samp=='' | samp=="," then samp= 10000 /* " " " " " " */
if datatype(seed, 'W') then call random ,,seed /*RANDOM seed given for repeatability ?*/
diy = 365 /*oralternative: 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 0 /*perform through 2 ──► duplicate size*/
do samp; @.=0 0 /*perform some number of trials. */
do j=0 until @.day==g /*perform until G dup. birthdays found.*/
day= random(1, diyM) % 100 /*expand range RANDOM number generation*/
@.day= @.day + 1 /*record the number of common birthdays*/
end /*j*/ /* [↓] adjust for the DO loop index.*/
s=s s+ j /*add number of birthday hits to sum. */
end /*samp*/ /* [↓] % 1 rounds down the division.*/
start.g= s/samp % 1 - g /*define where the try─outs start. */
end /*g*/ /* [↑] get a rough estimate for %. */
say right('sample size is ' samp, 40); say /*display this run's sample size. */
say ' required trial % with required'
Line 1,565 ⟶ 1,567:
say ' ──────────── ─────── ──────────────────'
do g=2 to dups /*perform through 2 ──► duplicate size*/
do try=start.g until s/samp>=.5; s=0 0 /* " try─outs until average ≥ 50%.*/
do samp; @.=0 0 /* " some number of trials. */
do try; day= random(1, diyM) % 100 /* " until G dup. birthdays found.*/
@.day= @.day + 1 /*record the number of common birthdays*/
if @.day==g then do; s=s+1; leave; end /*found enough G (birthday) hits ? */
end /*try;*/
Cookies help us deliver our services. By using our services, you agree to our use of cookies.