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 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: Simple hash or pseudo-random function Date: Tue, 17 Jul 2018 08:09:08 +0200 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: <5a66cd8a-e11d-4a59-bce1-8cc693b4160a@googlegroups.com> <979df026-05c7-447b-b5eb-010d85d61813@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 17 Jul 2018 06:09:09 -0000 (UTC) Injection-Info: h2725194.stratoserver.net; posting-host="698520962365dcca3ecfe855d716fba1"; logging-data="6515"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+2MuEz6OPOnthhRV752bzh/QR4Sf5C+kM=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 Cancel-Lock: sha1:d8kpThdrvd+q780H/qcn6DdmA5g= In-Reply-To: <979df026-05c7-447b-b5eb-010d85d61813@googlegroups.com> Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:53862 Date: 2018-07-17T08:09:08+02:00 List-Id: On 07/16/2018 11:14 PM, gautier_niouzes@hotmail.com wrote: > > The 64-bit value is the *input* and the output is a function of that input only. > e.g. > 10562032 gives always 211 > 31375393 gives always 31 > 85232830 gives always 172 > NB: the input codes can appear in a different order, so a pseudo-random *sequence* cannot be used. > > I've tested different RNG's by initializing them with the input code and using only the first pseudo-random value using that seed. The good news is that they seem uniformly distributed even with successive seed values, but they are not random enough when seeds are similar. I'll check Marius' solution, or a hash function like CRC. That makes things clearer. I would have suggested what you tried. Perhaps you could modify it a bit so that, for a value N, you discard the 1st N rem M values from the RNG before generating the output for N, and see if that improves the randomness of the results. M should be small enough that this is fast enough for your requirements but large enough to be significantly different from what you've already tried. 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. -- Jeff Carter "What's the amount of the insult?" Never Give a Sucker an Even Break 104