Numeric, reversible, pseudorandom number generator, for very large numbers

和自甴很熟 提交于 2020-01-04 02:41:29

问题


Does anyone know of an algorithm that fits all of the above criteria? I need to specify a seed number, and a range that I want the output numbers to fall under (which will also be the range that the input numbers are in). This function also needs to have a counterpart that reverses the operation.

for example:

I pass the seed 5, and the range 5-35, then I receive the number 27. I can then pass this into a function that reverses the operation, using the same range, that will give me the number 5 back.

I cannot store the original numbers, nor can I iterate through a list of the input numbers. This does not have to be encryption strength, and it has to be as fast as possible.

The only thing I can think of that sort of fits this description is an encryption algorithm. Even a point in the right direction would be awesome.

EDIT

I am trying to find a way to represent a set of random (looking) numbers that is too large to hold in memory (possibly 3e12 numbers), and then test if certain ranges of numbers show up in that set.

for example. If I have a function that gives me the random set (4, 22, 7, 343, 67, 38, 2), I want to be able to say, give me the numbers from that set that are between 1 and 30, and get the set (4, 22, 7, 2) back.


回答1:


As RB said, you need an encryption, not a RNG. With a given key, the encryption is reversible. The key could incorporate the seed and the range as well if you want different results from the same seed with a changed range.

Range sizes are a different problem. For a 32 bit range use DES. For 64 bit use AES. For other ranges either write your own simple Feistel cypher or use the Hasty Pudding cypher, which is defined for all sizes.

Whatever underlying cypher you use, you can always use the Hasty Pudding method of finding a number in the appropriate range: just keep encyphering the output until it is within the required range. Once you have something of the appropriate size you can add the lower bound back in to get your required number. So for your range of 5 to 35, you would generate a number in [0..30] and add 5.

ETA: Having thought about your problem a bit more, you can't use the seed as part of the key. If you do you won't be able to reconstruct the key to decrypt your random number. You can only use data in the key that you will know when you start reversing the process.

You will also need a way of recognising the seed when you come to it. As you decrypt you will get a series of numbers; you need a way to pick out which one was the original seed. Perhaps you could constrain the seed to be within the range specified (or its zero-based equivalent) and pick the first in the decryption series that falls within the correct range.




回答2:


No, this is not possible. A random number generator could generate the same output from multiple different seeds (in that respect it is like a hashing algorith generating the same outputs from different inputs).

For example, a PRNG might work like this (psuedo-code):

PRNG randomWithSeed5 = new PRNG(seed: 5);
PRNG randomWithSeed6 = new PRNG(seed: 6);
PRNG randomWithSeed7 = new PRNG(seed: 7);

randomWithSeed5.NextInt(range: 5-50); //returns 20
randomWithSeed6.NextInt(range: 5-50); //returns 20
randomWithSeed7.NextInt(range: 5-50); //returns 32

At this point, it's clear that it's not possible to write a Decode algorithm that, given an input of 20, could return the correct seed - you could not decide if 5 or 6 would be the correct answer.

It sounds like an encryption algorithm would much better match your needs - I don't even understand why random number generators are involved.



来源:https://stackoverflow.com/questions/15413686/numeric-reversible-pseudorandom-number-generator-for-very-large-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!