Birthday problem: Difference between revisions
Content added Content deleted
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: | Line 1,265: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===version 1=== |
===version 1=== |
||
The method used is to find the average number of people to share a |
The method used is to find the average number of people to share a birthday, and then use the '''floor''' of that |
||
<br> |
<br>value (less the group size) as a starting point to find a new group size with an expected size that exceeds |
||
<br>50% |
<br>50% duplicate birthdays of the required size. |
||
<lang rexx>/*REXX |
<lang rexx>/*REXX pgm examines the birthday problem via random # simulation (with specifable parms)*/ |
||
parse arg |
parse arg dups samp seed . /*get optional arguments from the CL. */ |
||
if |
if dups=='' | dups=="," then dups= 10 /*Not specified? Then use the default.*/ |
||
if samp=='' | samp== |
if samp=='' | samp=="," then samp= 10000 /* " " " " " " */ |
||
if datatype(seed,'W') |
if datatype(seed, 'W') then call random ,,seed /*RANDOM seed given for repeatability ?*/ |
||
diy =365 |
diy =365 /*or: diy=365.25 */ /*the number of Days In a Year. */ |
||
diyM=diy*100 /*this expands the RANDOM (BIF) range.*/ |
diyM=diy*100 /*this expands the RANDOM (BIF) range.*/ |
||
do g=2 to dups; s=0 /*perform through 2 ──► duplicate size*/ |
|||
do samp; @.=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=@.day + 1 /*record the number of common birthdays*/ |
||
end /*j*/ /* [↓] adjust for the DO loop index.*/ |
|||
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. */ |
|||
⚫ | |||
⚫ | |||
start.g=s/samp%1-g /*define where the try-outs start. */ |
|||
say ' required trial % with required' |
|||
say ' duplicates size common birthdays' |
|||
say |
|||
say ' ──────────── ─────── ──────────────────' |
|||
⚫ | |||
do g=2 to dups /*perform through 2 ──► duplicate size*/ |
|||
do try=start.g until s/samp>=.5; s=0 /* " try─outs until average ≥ 50%.*/ |
|||
do samp; @.=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 ? */ |
|||
⚫ | |||
do samp; @.=0 /*perform some number of trials. */ |
|||
⚫ | |||
do try /*perform until G dup. birthdays found.*/ |
|||
end /*try=start.g*/ /* [↑] where the try─outs happen. */ |
|||
⚫ | |||
@.day=@.day+1 /*record the number of common birthdays*/ |
|||
end /*g*/ /*stick a fork in it, we're all done. */</lang> |
|||
⚫ | |||
s=s+1 /*another common birthday found. */ |
|||
leave /* ··· and stop looking for more. */ |
|||
⚫ | |||
⚫ | |||
if s/samp>.5 then leave /*if the average is > 50%, then stop. */ |
|||
⚫ | |||
⚫ | |||
end /*g*/ /*stick a fork in it, we're all done. */</lang> |
|||
⚫ | |||
<pre> |
<pre> |
||
sample size is |
sample size is 10000 |
||
required |
required trial % with required |
||
duplicates size common birthdays |
|||
──────────── ─────── ────────────────── |
|||
──────── ───── ──────────────── |
|||
2 23 50. |
2 23 50.2300% |
||
3 |
3 87 50.2400% |
||
4 187 50. |
4 187 50.3800% |
||
5 |
5 312 50.0100% |
||
6 |
6 458 50.5200% |
||
7 |
7 622 50.3900% |
||
8 798 50. |
8 798 50.1700% |
||
9 |
9 984 50.5700% |
||
10 |
10 1182 51.4000% |
||
</pre> |
</pre> |
||