A
A
Alexander2021-03-24 13:07:33
Mathematics
Alexander, 2021-03-24 13:07:33

Maybe there is a uniform distribution algorithm based on the SHA256 hash?

There is an array [1... n] of objects. The number of elements can increase, i.e. n gradually increases.
We get an arbitrary SHA256 hash. I would like to have some algorithm that determines the object number from the array for this hash, so that when the next hash arrives, a new arbitrary number in the array is taken, but the following conditions are met:
1. The distribution should be close to uniform
2. The method should be reproducible, i.e. when a specific hash is dropped, must return the same position in the array every time this hash is dropped
3. It is possible for different hashes to correspond to the same position in the array, provided condition 1 is met.
4. There will be more than n hashes.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rsa97, 2021-03-24
@Rsa97

(hash % n) + 1

1
15432, 2021-03-24
@15432

idx = Hash % n

W
Wataru, 2021-03-24
@wataru

If you need the hashes to give the same positions after increasing n, then this cannot be done evenly without remembering. Because let n=10 initially - then ANY hash should give a value less than 10. And then when n increases, any hash should still give 0-9. Even if n becomes 100000, the added positions cannot be used, except for the first 10.
If you come up with a hash table, then when n increases, all existing hashes are recalculated with a new n and all elements move to new positions. As if n has always been like that.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question