Assigning a random natural number to every given natural number

Viewed 62

I need a function which assigns to every natural number on [0, infinity] a random corresponding natural number. For example, a function which takes a number and seed as a parameter, and gives me the random number that corresponds to the number I gave it for that given seed.

Ie,

To 5 it would assign 24.

To 2 it would assign 12589128948.

To 3 it would assign 12358.

So on, so on, so on.

I've been mulling over how to achieve this and don't really have any useful ideas. The algorithm must be seedable, so that I can generate the same set of integers to which each integer maps every time. The mapping does not necessarily have to be one-to-one.

Does anyone know if this is possible? How can it be achieved?

Thanks!

1 Answers

Sounds like what you’re looking for is a family of hash functions. Each hash function takes as input the integer, then computes some hash code that plays the role of the random assigned integer. Since hash functions always produce the same outputs from the same inputs, they have the repeatability that you’d like.

There are many ways to do this. If you have some fixed hash function f available to you, then you could use as your “key” some randomly chosen integer k. The number associated with n could then be computed as f(n + k).

Hope this helps!

Related