Birthday problem: Difference between revisions
Content added Content deleted
m (→version 1: changed whitespace and some comments; added wording to the REXX section header.) |
|||
Line 1,540: | Line 1,540: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===version 1=== |
===version 1=== |
||
The method used is to find the average number of people to share a birthday, and then use the '''floor''' |
The root finding method used is to find the average number of people to share a birthday, and then use the '''floor''' |
||
<br>value (less the group size) as a starting point to find a new group size with an expected size that exceeds |
<br>of that value (less the group size) as a starting point to find a new group size with an expected size that exceeds |
||
<br>50% duplicate birthdays of the required size. |
<br>50% duplicate birthdays of the required size. |
||
⚫ | |||
This REXX version doesn't need a precalculated group size to find the percentage required to exceed 50%. |
|||
⚫ | |||
parse arg dups samp seed . /*get optional arguments from the CL. */ |
parse arg dups samp seed . /*get optional arguments from the CL. */ |
||
if dups=='' | dups=="," then dups= 10 /*Not specified? Then use the default.*/ |
if dups=='' | dups=="," then dups= 10 /*Not specified? Then use the default.*/ |
||
if samp=='' | samp=="," then samp= 10000 /* " " " " " " */ |
if samp=='' | samp=="," then samp= 10000 /* " " " " " " */ |
||
if datatype(seed, 'W') then call random ,,seed /*RANDOM seed given for repeatability ?*/ |
if datatype(seed, 'W') then call random ,,seed /*RANDOM seed given for repeatability ?*/ |
||
diy =365 |
diy = 365 /*alternative: diy=365.25*/ /*the number of Days In a Year. */ |
||
diyM=diy*100 |
diyM= diy*100 /*this expands the RANDOM (BIF) range.*/ |
||
do g=2 to dups; s= |
do g=2 to dups; s= 0 /*perform through 2 ──► duplicate size*/ |
||
do samp; @.= |
do samp; @.= 0 /*perform some number of trials. */ |
||
do j=0 until @.day==g |
do j=0 until @.day==g /*perform until G dup. birthdays found.*/ |
||
day=random(1, diyM) |
day= random(1, diyM) % 100 /*expand range RANDOM number generation*/ |
||
@.day=@.day + 1 |
@.day= @.day + 1 /*record the number of common birthdays*/ |
||
end /*j*/ |
end /*j*/ /* [↓] adjust for the DO loop index.*/ |
||
s= |
s= s+j /*add number of birthday hits to sum. */ |
||
end /*samp*/ |
end /*samp*/ /* [↓] % 1 rounds down the division.*/ |
||
start.g= s/samp % 1 - g |
start.g= s/samp % 1 - g /*define where the try─outs start. */ |
||
end /*g*/ |
end /*g*/ /* [↑] get a rough estimate for %. */ |
||
say right('sample size is ' samp, 40); say /*display this run's sample size. */ |
say right('sample size is ' samp, 40); say /*display this run's sample size. */ |
||
say ' required trial % with required' |
say ' required trial % with required' |
||
Line 1,565: | Line 1,567: | ||
say ' ──────────── ─────── ──────────────────' |
say ' ──────────── ─────── ──────────────────' |
||
do g=2 to dups /*perform through 2 ──► duplicate size*/ |
do g=2 to dups /*perform through 2 ──► duplicate size*/ |
||
do try=start.g until s/samp>=.5; s= |
do try=start.g until s/samp>=.5; s= 0 /* " try─outs until average ≥ 50%.*/ |
||
do samp; @.= |
do samp; @.= 0 /* " some number of trials. */ |
||
do try; day=random(1, diyM) % 100 |
do try; day= random(1, diyM) % 100 /* " until G dup. birthdays found.*/ |
||
@.day=@.day + 1 |
@.day= @.day + 1 /*record the number of common birthdays*/ |
||
if @.day==g then do; s=s+1; leave; end /*found enough G (birthday) hits ? */ |
if @.day==g then do; s=s+1; leave; end /*found enough G (birthday) hits ? */ |
||
end /*try;*/ |
end /*try;*/ |