From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 2002:a02:5696:: with SMTP id u22-v6mr2578381jad.5.1531921087795; Wed, 18 Jul 2018 06:38:07 -0700 (PDT) X-Received: by 2002:aca:75c9:: with SMTP id q192-v6mr1276549oic.3.1531921087326; Wed, 18 Jul 2018 06:38:07 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!feeder.eternal-september.org!news.uzoreto.com!weretis.net!feeder6.news.weretis.net!feeder.usenetexpress.com!feeder-in1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!g2-v6no2347421itf.0!news-out.google.com!k71-v6ni617itk.0!nntp.google.com!g2-v6no2347420itf.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Wed, 18 Jul 2018 06:38:07 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=57.79.21.10; posting-account=gRqrnQkAAAAC_02ynnhqGk1VRQlve6ZG NNTP-Posting-Host: 57.79.21.10 References: <5a66cd8a-e11d-4a59-bce1-8cc693b4160a@googlegroups.com> <979df026-05c7-447b-b5eb-010d85d61813@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <1a7509e9-dfb2-4939-9ae2-b522a4d41bae@googlegroups.com> Subject: Re: Simple hash or pseudo-random function From: gautier_niouzes@hotmail.com Injection-Date: Wed, 18 Jul 2018 13:38:07 +0000 Content-Type: text/plain; charset="UTF-8" Xref: reader02.eternal-september.org comp.lang.ada:53891 Date: 2018-07-18T06:38:07-07:00 List-Id: 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.