HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Enumerating sets in a random order

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
randomsetsorderenumerating

Problem

I have multiples arrays. I'd like to enumerate all sets containing exactly one item from each array in a (pseudo-)random order, without explicitly building the array of all sets.

Any solution, even with poor pseudo-randomization, is welcome.

EDIT: Example for clarity:

Say I have arrays

A = { a1, a2 }, B = { b1, b2 }, C = { c1 }


I want to enumerate all the sets containing exactly one ax, bx and cx

{ a1, b1, c1 }, { a1, b2, c1 }, { a2, b1, c1 }, { a2, b2, c1 }


I want to enumerate them in a random order. The naïve solution would be to enumerate them in an array, then shuffle this array. But the solution is huge and I don't want to store it explicitely in memory.

Solution

Suppose that you have arrays $A_1,\ldots,A_d$ of sizes $n_1,\ldots,n_d$. The total number of tuples is thus $n_1 \times \cdots \times n_d$. Given a number in the range $1,\ldots,n_1 \times \cdots \times n_d$, you can decipher it into a tuple (I leave this part to you). It now remains to compute a pseudorandom permutation of the numbers $1,\ldots,n_1 \times \cdots \times n_d$, another task I leave to you.

Context

StackExchange Computer Science Q#63904, answer score: 4

Revisions (0)

No revisions yet.