From 7808f4c272ef77b3a9972af180bb8b3841ae3c78 Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Mon, 3 Feb 2020 16:02:28 +1100 Subject: [PATCH] hash.wyhash: implement rand function from wyhash original impl --- vlib/hash/wyhash/rand.v | 14 ++++++++++++++ vlib/hash/wyhash/wyhash_test.v | 12 ++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 vlib/hash/wyhash/rand.v diff --git a/vlib/hash/wyhash/rand.v b/vlib/hash/wyhash/rand.v new file mode 100644 index 0000000000..3f6e9a86b6 --- /dev/null +++ b/vlib/hash/wyhash/rand.v @@ -0,0 +1,14 @@ +// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved. +// Use of this source code is governed by an MIT license +// that can be found in the LICENSE file. +module wyhash + +pub fn rand_u64(seed &u64) u64 { + mut seed0 := seed + unsafe{ + mut seed1 := *seed0 + seed1+=wyp0 + *seed0 = seed1 + return wymum(seed1^wyp1, seed1) + } +} diff --git a/vlib/hash/wyhash/wyhash_test.v b/vlib/hash/wyhash/wyhash_test.v index 1d7bd3b675..414b05dce5 100644 --- a/vlib/hash/wyhash/wyhash_test.v +++ b/vlib/hash/wyhash/wyhash_test.v @@ -27,3 +27,15 @@ fn test_wyhash() { assert got == test.expected } } + +fn test_rand_u64() { + seed := u64(111) + mut rand_nos := []u64 + for _ in 0..40 { + rand_no := wyhash.rand_u64(&seed) + for r in rand_nos { + assert rand_no != r + } + rand_nos << rand_no + } +} \ No newline at end of file