<p dir="auto"><strong>Minimal working example</strong> (Tested on Julia 1.5 and 1.6.)</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="julia&gt; A = rand(Float32, 2^20);
julia&gt; length(A)-length(unique(A))"><pre class="notranslate">julia<span class="pl-k">&gt;</span> A <span class="pl-k">=</span> <span class="pl-c1">rand</span>(Float32, <span class="pl-c1">2</span><span class="pl-k">^</span><span class="pl-c1">20</span>);
julia<span class="pl-k">&gt;</span> <span class="pl-c1">length</span>(A)<span class="pl-k">-</span><span class="pl-c1">length</span>(<span class="pl-c1">unique</span>(A))</pre></div>
<p dir="auto"><strong>Expected behavior</strong><br>
The expected number of duplicates when drawing <code class="notranslate">n</code> times at random from a set of <code class="notranslate">N</code> elements with replacement is equal to (see e.g. <a href="https://arxiv.org/pdf/2003.08009.pdf" rel="nofollow">Proposition 4.1 in this paper</a>)</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="expected_number_of_duplicates(N, n) = n - N * (1 - (1 - 1/N)^n)"><pre class="notranslate"><span class="pl-en">expected_number_of_duplicates</span>(N, n) <span class="pl-k">=</span> n <span class="pl-k">-</span> N <span class="pl-k">*</span> (<span class="pl-c1">1</span> <span class="pl-k">-</span> (<span class="pl-c1">1</span> <span class="pl-k">-</span> <span class="pl-c1">1</span><span class="pl-k">/</span>N)<span class="pl-k">^</span>n)</pre></div>
<p dir="auto">For drawing a uniform <code class="notranslate">Float32</code>, we therefore expect</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="julia&gt; expected_number_of_duplicates(2^32, 2^20)
127.98946189880371"><pre class="notranslate">julia<span class="pl-k">&gt;</span> <span class="pl-c1">expected_number_of_duplicates</span>(<span class="pl-c1">2</span><span class="pl-k">^</span><span class="pl-c1">32</span>, <span class="pl-c1">2</span><span class="pl-k">^</span><span class="pl-c1">20</span>)
<span class="pl-c1">127.98946189880371</span></pre></div>
<p dir="auto"><strong>Actual behavior</strong><br>
I obtain numbers that are on the order of <code class="notranslate">60000</code>.</p>
<p dir="auto">Pursuant to discussion here: <a href="https://discourse.julialang.org/t/map-pmap-and-scalar-argument/30089" rel="nofollow">https://discourse.julialang.org/t/map-pmap-and-scalar-argument/30089</a></p>
<p dir="auto">Scalars, provided to a function like <code class="notranslate">map</code> or <code class="notranslate">pmap</code>, leads to unexpected behavior:</p>
<p dir="auto"><code class="notranslate">map(+, 1:2, 1) # [2]</code></p>
<p dir="auto"><code class="notranslate">map(+, 1:2, [1]) # ERROR: DimensionMismatch</code></p>
<p dir="auto">A user would naively think a scalar would be broadcast or rejected. Treating it as if the user just wanted to process the first element of each argument would be unlikely to be intended.</p>
<p dir="auto">Based on discussion linked above, it appears to be because scalars are iterable. The code for consuming iterables uses <code class="notranslate">zip()</code> to stitch the arguments together, which takes as many as it can and discards the remainder.</p>
<p dir="auto">For comparison, here are how some other inputs are treated:</p>
<p dir="auto"><code class="notranslate">map(+, 1:2, (1,)) # [2]</code></p>
<p dir="auto"><code class="notranslate">map(+, 1:2, (1,2)) # [2;4]</code></p>
<p dir="auto"><code class="notranslate">map(+, 1:2, (1,2,3)) # [2;4]</code></p>
<p dir="auto"><code class="notranslate">map(+, 1:2, (i for i in 1:1)) # ERROR: DimensionMismatch</code></p>
<p dir="auto"><code class="notranslate">map(+, 1:2, (i for i in 1:2)) # [2;4]</code></p>
<p dir="auto">These are also surprising to me, since documentation suggests a generator is an iterable, but its size is being enforced as if it were an array. Meanwhile, tuples which have a fixed number of elements are treated like an iterator of unknown length.</p>