comp.lang.ada
 help / color / mirror / Atom feed
From: gautier_niouzes@hotmail.com
Subject: Re: Simple hash or pseudo-random function
Date: Wed, 1 Aug 2018 02:08:21 -0700 (PDT)
Date: 2018-08-01T02:08:21-07:00	[thread overview]
Message-ID: <9c40251b-4144-494a-bd50-661dee57b474@googlegroups.com> (raw)
In-Reply-To: <1a7509e9-dfb2-4939-9ae2-b522a4d41bae@googlegroups.com>

Just a followup on the last remark: as expected, GNAT's Hash function gives consecutive values for consecutive characters at the end of the string. People may want to append some constant string to get randomness in the hash code.

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Hash;

procedure Hash_test is
begin
  Put_Line (Ada.Strings.Hash ("Abcd")'Image);
  Put_Line (Ada.Strings.Hash ("Bbcd")'Image);
  Put_Line (Ada.Strings.Hash ("Cbcd")'Image);
  Put_Line ("--");
  Put_Line (Ada.Strings.Hash ("abCd")'Image);
  Put_Line (Ada.Strings.Hash ("abDd")'Image);
  Put_Line (Ada.Strings.Hash ("abEd")'Image);
  Put_Line ("--");
  Put_Line (Ada.Strings.Hash ("abcD")'Image);
  Put_Line (Ada.Strings.Hash ("abcE")'Image);
  Put_Line (Ada.Strings.Hash ("abcF")'Image);
end Hash_test;

 14682274
 795269473
 1575856672
--
 3516536994
 3516602593
 3516668192
--
 3518636130
 3518636131
 3518636132

NB: same behavior for ObjectAda (reverting here to pure Ada 2005 syntax):

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Hash;
with Ada.Containers; use Ada.Containers;

procedure Hash_test is
begin
  Put_Line (Hash_Type'Image (Ada.Strings.Hash ("Abcd")));
  Put_Line (Hash_Type'Image (Ada.Strings.Hash ("Bbcd")));
  Put_Line (Hash_Type'Image (Ada.Strings.Hash ("Cbcd")));
  Put_Line ("--");                                    
  Put_Line (Hash_Type'Image (Ada.Strings.Hash ("abCd")));
  Put_Line (Hash_Type'Image (Ada.Strings.Hash ("abDd")));
  Put_Line (Hash_Type'Image (Ada.Strings.Hash ("abEd")));
  Put_Line ("--");                                    
  Put_Line (Hash_Type'Image (Ada.Strings.Hash ("abcD")));
  Put_Line (Hash_Type'Image (Ada.Strings.Hash ("abcE")));
  Put_Line (Hash_Type'Image (Ada.Strings.Hash ("abcF")));
end Hash_test;

 803596578
 1584183777
 2364770976
--
 10484002
 10549601
 10615200
--
 12583138
 12583139
 12583140

  reply	other threads:[~2018-08-01  9:08 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
2018-08-01  9:08         ` gautier_niouzes [this message]
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