0

I want to output random looking numbers based on an input. If the same input is put in, the same output is given.

I don't want to pregenerate and store a bunch of random data, and I don't want it to take an O(n) amount of time to recover the nth index.

It does not need to be secure, cryptographically or otherwise, just enough to look random.

voutasaurus
  • 2,968
  • 2
  • 24
  • 37
Rich
  • 926
  • 1
  • 9
  • 17
  • Where/how would you like these stored would a database work? – e11i0t23 Jun 09 '18 at 21:19
  • Why store gigs of random numbers as opposed to return the next random number? – ChiefTwoPencils Jun 09 '18 at 21:19
  • Well exactly I don't want to store the whole array, just returns sections of it on request. – Rich Jun 09 '18 at 21:20
  • You would need to store the whole array somewhere to be able to give sections of it unless you want to just give a next newly generated section – e11i0t23 Jun 09 '18 at 21:21
  • You can seed the random number generator with, say, the current ticks of `Now` thereby creating relative randomness and generating + returning *n* consecutive random numbers. – ChiefTwoPencils Jun 09 '18 at 21:22
  • Why would I need to "store the whole array somewhere"? If it's generated algorithmically using an LCG then there is presumably a way to return any request at [offset, offset+size-1], I just don't want to have to iterate from 0 thru offset to do that. – Rich Jun 09 '18 at 21:23
  • Do you want to be able to read the same section more than once? – voutasaurus Jun 09 '18 at 21:32
  • Yes, and to read the same data back each time. – Rich Jun 09 '18 at 21:36
  • 1
    If you want random access, deterministic, jumbled-looking data, then I suggest using a hash function instead of storing anything. – voutasaurus Jun 09 '18 at 21:37
  • Would that hash the array index? [edited] – Rich Jun 09 '18 at 21:37
  • How big do you want the "sections" to be? – voutasaurus Jun 09 '18 at 21:38
  • What determines what "section" you want; is the size of the "section" dynamic? For example, can I get 10 from a section, then 20 where the first 10 are the same? – ChiefTwoPencils Jun 09 '18 at 21:39
  • Any size of read from a few bytes to the whole array. – Rich Jun 09 '18 at 21:40
  • https://en.wikipedia.org/wiki/SHA-3 has a variable output length so you could specify dynamically how long you want your "random" data to be. Just use the index as the input data and it'll be deterministic. – voutasaurus Jun 09 '18 at 21:41

2 Answers2

1

If you want a deterministic random-access function from an (index,length) pair to a random looking string of bytes you could use SHA3-N(index)[:length] where N is the first convenient number greater than length.

This would not behave identically to an actual array as reading indexes 1 (with length 10) and 5 (with length 10) would not have any overlap (which you'd expect from an array).

This is going to be slow and very inconvenient for N>512, so if you need longer strings you'll want to do multiple rounds. Something like SHA3-512(SHA3-512(index)[0:256])++SHA3-512(SHA3-512(index)[256:512]) to get something 1024bytes long.

Armed with the multiple rounds part you could use any hash function (e.g. SHA256, MD5) which might be more convenient.

I should note that this is definitely not secure and the output could easily be predicted by an adversary.

voutasaurus
  • 2,968
  • 2
  • 24
  • 37
0

Typically, a random number generator will generate the same sequence of pseudo-random numbers given the same seed. For example, such python code might be like so:

random.seed(1)
for i in range(1, 10):
    print(random.randint(1,100)

Will print the same list no matter how many times you invoke that code. Similarly, so will this:

random.seed(42)
for i in range(1, 10):
    print(random.randint(1,100)

If somehow you then describe the sections of your array as a seed (you could use a hash function to do this indeed) you can seed the generator with that value and reliably allow dynamic sizing of the list requested.

ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75