I am looking for an algorithm which stores a long list (possibly thousands) of random numbers and retrieves them efficiently. Typically the solution should not require any sorting of data i.e the numbers are stored as they are generated, And the storage space required should be less than that required for an array/hash of such numbers. New numbers can be added to the list.
Share
@Stemm has a very good idea of just storing the seed to the pseudo-random number generator (prng). The count of the numbers would also be required so that you would know how many times to call prng to retrieve them.
If you don’t have access to the seed or the numbers are otherwise random, then you might have another option. If your numbers are integers, not very big, and you know there are no duplicates, then consider storing them as bits. So, for example, if your longest value fits in a 2 byte int, then that value could be stored by using 1 bit. Some examples:
0 = 1.
4 = 10000 binary or 10 hex.
10 = 10000000000 binary.
If the largest value is 65535, which is the largest value that can fit inside of a 16 bit unsigned integer, then the amount of memory to hold all the values can be calculated as 65536 / 8 = 8192 bytes. If you are using Java, take a look at the
java.util.BitSetorjava.math.BigIntegerclasses to help do this.