<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div><span style="font-family:Calibri, Arial, Helvetica, sans-serif;font-size:12pt">To get 5 more bits:</span><br>
<span style="font-family:Calibri, Arial, Helvetica, sans-serif;font-size:12pt"></span><br>
</div>
<div>phash2(Term, 1 bsl 32)<span><br>
</span></div>
<div><span><br>
</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
You get less collisions, but it still does not get close to a uniform distribution for your example.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span><span>Test(500) -> 249993<br>
</span></span><span style="font-size:12pt"></span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
/Sverker</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> erlang-questions <erlang-questions-bounces@erlang.org> on behalf of Roger Lipscombe <roger@differentpla.net><br>
<b>Sent:</b> Wednesday, December 15, 2021 12:57 PM<br>
<b>To:</b> erlang-questions <erlang-questions@erlang.org><br>
<b>Subject:</b> phash2 isn't very resistant to collisions?</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">I'm sure this has come up previously, but all I could find talked about atoms.<br>
<br>
I know it's not intended to be cryptographically strong, but<br>
erlang:phash2 is woefully collidey with 2-tuples:<br>
<br>
Test = fun(N) -><br>
    L = [{X, Y} || X <- lists:seq(0, N - 1), Y <- lists:seq(0, N - 1)],<br>
    Expected = N * N,<br>
    Expected = length(L),<br>
    Expected = length(lists:usort([erlang:phash2(Term) || Term <- L]))<br>
end.<br>
<br>
1> Test(10).<br>
100<br>
<br>
2> Test(100).<br>
** exception error: no match of right hand side value 9999<br>
<br>
3> Test(99).<br>
** exception error: no match of right hand side value 9800<br>
<br>
4> Test(500).<br>
** exception error: no match of right hand side value 249763<br>
<br>
What alternatives are there to phash2, if I still care about performance?<br>
</div>
</span></font></div>
</body>
</html>