-2

Is it possible to create $O(1)$ memory consuming algorithm, which is generating non-repeating pseudo random numbers?

I can remember numbers in the hash set and it will be $O(1)$ time, but the set will grow as numbers generated as $O(N)$. Is it possible to do $O(1)$?

UPDATE

Narrowing the question: we have parameter $N$. We need to generate all numbers from 1 to $N$ without repetition in (pseudo) random order.

UPDATE 2

May be there are reversible hash functions? I would compute just $H(i)$ then.

Dims
  • 117
  • 6

5 Answers5

2

First, you should precisely define what you mean by random. One way to interpret you question is as follows. Given some fixed number $N$, you wish to sample elements from the uniform distribution over the set $\{1,...,N\}$. Let us denote by $S$ the set of samples seen so far, then we want to support a $Gen$ command, which upon calling returns a uniformly chosen element from $\{1,...,N\}\setminus S$. $Gen$ has access to additional memory $q\in\{0,1\}^*$ (the state), which can be altered upon invocation.

It can be easily shown that you cannot achieve the above goal while keeping $q$'s length bounded by some constant. Suppose $|q|\le c$ for some $c\in\mathbb{N}$, and choose $N$ such that $N > 2^c$. Consider the random variable $x_1,...,x_N$ obtained by invoking $Gen$ $N$ times. The difference in the distributions of $x_i,x_j$ depends solely on $q_i,q_j$ (the state during the sampling). Since there are less than $N$ possible states (values of $q$), there exists $i\neq j$ such that $x_i,x_j$ are drawn from the same distribution, contradiction.

Ariel
  • 13,614
  • 1
  • 22
  • 39
1

Take any bijective memoryless function $f$ that scrambles the bits and use the sequence $f(i)$ for $i\in[0,N)$.

For instance, Gray codes.

0

Many pseudo-random number generating algorithms naturally produce a very long sequence of unique numbers, and the repeat the sequence. Of course if you have a sequence length of $2^{128}$ then the sequence is not repeating.

So the solution: If you need non-repeating numbers in the range 1 to n you use a PRNG with a cycle not longer than n. If n is small, it’s not going to be very random.

gnasher729
  • 32,238
  • 36
  • 56
0

Effectively what you want to compute is some permutation of a sequence of numbers $\{1, \ldots, n\}$.

Let $p_1$ and $p_2$ be two prime numbers. It can be shown that the sequence

$$(p_1)^1 \text{ mod } p_2, (p_1)^2 \text{ mod } p_2, (p_1)^3 \text{ mod } p_2, \ldots, (p_1)^{p_2} \text{ mod } p_2$$

does not have repetitions. So if you want to get a pseudo-random permutation of the numbers $\{1, \ldots, n\}$, you can set $p_2$ to be a prime number higher than $n$ and select $p_1$ somehow (e.g., close to 1/4 of $p_1$). Then, when enumerating the sequence above, you just drop all elements greater than n.

Note that the resulting sequence is only somewhat random according to some statistical tests.

DCTLib
  • 2,817
  • 17
  • 13
0

No.

Any deterministic algorithm with bounded memory has a finite number of states and every state always leads to the same next state.

So whatever you generate will be periodical, with a period not excceding $2^S$for $S$ bits of memory.

David Richerby
  • 82,470
  • 26
  • 145
  • 239