Search code examples
mathrandomnumber-theory

Generate very very large random numbers


How would you generate a very very large random number? I am thinking on the order of 2^10^9 (one billion bits). Any programming language -- I assume the solution would translate to other languages.

I would like a uniform distribution on [1,N].

My initial thoughts:

--You could randomly generate each digit and concatenate. Problem: even very good pseudorandom generators are likely to develop patterns with millions of digits, right?

  • You could perhaps help create large random numbers by raising random numbers to random exponents. Problem: you must make the math work so that the resulting number is still random, and you should be able to compute it in a reasonable amount of time (say, an hour).

  • If it helps, you could try to generate a possibly non-uniform distribution on a possibly smaller range (using the real numbers, for instance) and transform. Problem: this might be equally difficult.

Any ideas?


Solution

  • Generate log2(N) random bits to get a number M, where M may be up to twice as large as N. Repeat until M is in the range [1;N].

    Now to generate the random bits you could either use a source of true randomness, which is expensive.

    Or you might use some cryptographically secure random number generator, for example AES with a random key encrypting a counter for subsequent blocks of bits. The cryptographically secure implies that there can be no noticeable patterns.