基本的に、数値を 0 ~ 999 の値にマップするハッシュ関数を求めています。
それを構築するには、最初にハッシュ関数を使用してマッピング先の値の体系的なパターンを取り除き、次に mod を使用して出力を 0 ~ 999 の値に制限します。
そのアイデアの R 実装は次のとおりです。
library(digest)
set.seed(1)
(x <- sample(1e9, size=6))
# [1] 265508664 372123900 572853364 908207790 201681932 898389685
## To hash R's internal representation of these numbers
strtoi(substr(sapply(x, digest), 28, 32), 16L) %% 1e3
# [1] 552 511 233 293 607 819
## Or, for a hash mapping that's comparable to other programs' md5 hash
## implementations
strtoi(substr(sapply(as.character(x), digest, serialize=FALSE),28,32),16L) %% 1e3
# [1] 153 180 892 294 267 807
そのワンライナーを細かく分割すると、それが何をするのかが少し明確になるはずです:
## Compute md5 hash of R representation of each input number
(sapply(x, digest))
# [1] "a276b4d73a46e5a827ccc1ad970dc780" "328dd60879c478d49ee9f3488d71a0af"
# [3] "e312c7f09be7f2e8391bee2b85f77c11" "e4ac99a3f0a904b385bfdcd45aca93e5"
# [5] "470d800a40ad5bc34abf2bac4ce88f37" "0008f4edeebbafcc995f7de0d5c0e5cb"
## Only really need the last few hex digits
substr(sapply(x, digest), 28, 32)
# [1] "dc780" "1a0af" "77c11" "a93e5" "88f37" "0e5cb"
## Convert hex strings to decimal integers
strtoi(substr(sapply(x, digest), 28, 32), 16L)
# [1] 903040 106671 490513 693221 560951 58827
## Map those to range between 0 and 999
strtoi(substr(sapply(x, digest), 28, 32), 16L) %% 1e3
# [1] 40 671 513 221 951 827