enumerating in pseudo random order

113 Views Asked by At

edit 3:

After more testing, I asked a new question with a jsfiddle

enumerating in pseudo random order - version 2

edit 2:

It's not a multiplication. It's more like a maze.

A given column in row 1 will be the column number of row 2, which again will be the column number of row 3, which will be the column number of row 4. This is the first number and also the column number for row 1, which is the column number for row 2, which is rhe column number for row 3. This is the second number and als the column number for row 1, which is the column number for row 2. This is the 3rd number and also the column number for row 1. Which is the 4th number.

Then the fist row is rotated 1 position and the process is repeated for the 2nd series of 4 digits. If the first row has been rotated 4 times, row 2 will also be rotated 1 position, etc.

The matrix will walk trough 4x4x4x4 = 256states, giving me 256 numbers, after which the loop is quit. The loop doesn't loop 1000 times.

I have a feeling that my idea is flawed. The problem is that it works for a few cases. If I can predict which matrix works and which doesn't then I also get what I want. But I don't know if this can be known without trying all possibilities.


edit 1:

I want to obfuscate the numbers somewhat, but it's not for cryptography. What I'm trying to prevent is a pre-calculated list, because I don't know how large the list is going to be.


original post:

Sorry for the code, but it's the only way to explain what I'm looking for, because I don't know what it's named.

I'm trying to do something like this (note that it is in base 4)

0000 -> 3012 
0001 -> 2310
0002 -> 3021
0003 -> 1230
0010 -> 3210
...
3332 -> 2103
3333 -> 0321

in which the required number is a unique number determined by a matrix (see example code below). Is there a name for what I'm trying to do? Or is there a way to prove/determine which matrix will enumerate all the unique numbers and not generates any duplicates?

var foo = [];

// var A = [0, 2, 1, 3]; // many the same
// var B = [1, 2, 0, 3];
// var C = [0, 1, 2, 3];
// var D = [1, 2, 0, 3];

var A = [0, 2, 1, 3]; // all unique
var B = [1, 2, 0, 3];
var C = [0, 1, 2, 3];
var D = [1, 2, 3, 0];

var a = 0;
var b = 0;
var c = 0;
var d = 0;

for (var i = 0; i < 1000; i = i + 1)
{
    var str1 = "";
    var str2 = "";
    var str3 = "";
    var str4 = "";

    var k = 0;

    k = D[k]; k = C[k]; k = B[k]; k = A[k]; str1 = str1 + k;
    k = D[k]; k = C[k]; k = B[k];           str2 = str2 + k;
    k = D[k]; k = C[k];                     str3 = str3 + k;
    k = D[k];                               str4 = str4 + k;

    foo.push("" + str1 + str2 + str3 + str4);

    a = a + 1;

    A.push(A.shift()); // shift row A

    if (a === A.length) { a = 0; b = b + 1; B.push(B.shift()); }
    if (b === B.length) { b = 0; c = c + 1; C.push(C.shift()); }
    if (c === C.length) { c = 0; d = d + 1; D.push(D.shift()); }
    if (d === D.length) { console.log("break"); break; }
}

console.log("");
foo.sort();
for (var i = 0; i < foo.length; i = i + 1) { console.log(foo[i]); }
for (var i = foo.length-1; i > 0; i = i - 1) { if (foo[i] === foo[i-1]) { console.log(i-1, i, foo[i]); } }
1

There are 1 best solutions below

3
On

In your example you have $4^4=256$ inputs and $4!=24$ outputs, so the mapping will be more than $10$ to $1$. A matrix will also have linearity properties that you probably don't want if you are doing cryptography. Maybe the best is to generate a random number up to $24!$ and use that to select an ordering of the $24$ outputs. Converting the random number to the list of outputs can be quite fast, even though the random is quite large.