comp.lang.ada
 help / color / mirror / Atom feed
From: gautier_niouzes@hotmail.com
Subject: Re: Simple hash or pseudo-random function
Date: Wed, 18 Jul 2018 06:38:07 -0700 (PDT)
Date: 2018-07-18T06:38:07-07:00	[thread overview]
Message-ID: <1a7509e9-dfb2-4939-9ae2-b522a4d41bae@googlegroups.com> (raw)
In-Reply-To: <pik164$6bj$1@dont-email.me>

Am Dienstag, 17. Juli 2018 08:09:10 UTC+2 schrieb Jeffrey R. Carter:

> Another possibility would be to try (calling your 64-bit type U64)
> 
> subtype S8 is String (1 .. 8);
> 
> function To_S8 is new Ada.Unchecked_Conversion (Source => U64, Target => S8);
> 
> Ada.Strings.Hash (To_S8 (N) );
> 
> If Hash is a decent hash function ("It should be unlikely for similar strings to 
> return the same value.") then it might be sufficiently random.

Brillant, works as expected (after a few adjustments on the final range to be really uniform).

Looking at GNAT's s-strhas.adb :

      H := 0;
      for J in Key'Range loop
         H := Char_Type'Pos (Key (J))
                + Shift_Left (H, 6) + Shift_Left (H, 16) - H;
      end loop;

I guess it is "random" enough on small changes of Key (Key'First .. Key'Last - 1) or on small changes (+1 or -1) of N if U64's are stored as little-endian.


  reply	other threads:[~2018-07-18 13:38 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-16  8:20 Simple hash or pseudo-random function gautier_niouzes
2018-07-16 13:34 ` Shark8
2018-07-17  4:44   ` robin.vowels
2018-07-16 14:52 ` Marius Amado-Alves
2018-07-16 20:59   ` gautier_niouzes
2018-07-16 17:17 ` Jeffrey R. Carter
2018-07-16 21:14   ` gautier_niouzes
2018-07-17  6:09     ` Jeffrey R. Carter
2018-07-18 13:38       ` gautier_niouzes [this message]
2018-08-01  9:08         ` gautier_niouzes
2018-07-17  0:40 ` Dan'l Miller
2018-07-17  6:44 ` Paul Rubin
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox