1

So I need to be able to scramble numbers around but have the outcome always be consistent. Meaning, if I scrambled 1234 and it gave me something like 2143, if I ran the scramble function again it would result in 2143 again.

Is anyone aware of a way to do this?

Mpsteve137
  • 49
  • 1
  • 1
  • 4
  • You might try generating a random permutation which you then apply to the input array. This way you can repeatedly apply the same permutation as needed. – andand Mar 31 '21 at 15:42
  • Why would you randomize the function if you need consistency? Is there a specific length to these numbers? fixed or variable? Also, can you give a bit more context on the bigger problem? – Edgardo Rodríguez Mar 31 '21 at 15:46
  • There is not a specific length to the numbers. They are fixed. And I guess the function I included can be ignored now that I think about it. The bigger problem is that there are multiple forms that have the same field, and the field needs to be scrambled but remain consistently scrambled across all of the forms. – Mpsteve137 Mar 31 '21 at 15:50
  • Do you want this consistent for the same content of the array or for the same length of the array? For example `1234` might be `2143` but if it's just length-based, then `5678` would produce `6587` (items are shuffled to the same positions as the first one) but if it's content based, it might produce something different altogether. – VLAZ Mar 31 '21 at 15:55
  • @VLAZ definitely content based. – Mpsteve137 Mar 31 '21 at 16:06
  • If you want it content based, then how about compute a hash on the content and use that as the seed for your random number generator. – andand Mar 31 '21 at 16:27
  • This question could be a bit clearer, but it is kinda of an interesting problem. I thought the same as @andand: set the seed based on the content. But, the javascript Math.random() does not allow to set the seed :/ – zelite Mar 31 '21 at 16:30
  • @zelite true but there are libraries for RNG, like [this one](http://davidbau.com/archives/2010/01/30/random_seeds_coded_hints_and_quintillions.html) (first one I happened upon). Can't recommend any as I've not used any but they are out there. – VLAZ Mar 31 '21 at 16:42
  • The P in PRNG makes it deterministic. (Pseudo-random number generation starts with a seed) Choose your random number, which will correspond to the arrangement. Then do a Knuth Shuffle using a PRNG instance seeded with that number. – Wyck Mar 31 '21 at 17:31

1 Answers1

3

One simple solution would be to treat numbers as character arrays and shuffle them using a seedable random generator, e.g. an LCG, using the number itself as a seed.

function scramble(n) {
    let rnd = n;

    let a = 1103515245,
        c = 12345,
        m = 1 << 30;

    let s = [...String(n)];

    for (let i = s.length - 1; i > 0; i--) {
        rnd = (rnd * a + c) % m;

        let r = rnd % i;
        let t = s[i];
        s[i] = s[r];
        s[r] = t;
    }

    return Number(s.join(''));
}


for (let n = 1234; n < 1284; n++) {
    let h = scramble(n)
    console.log(n, h)
}

References:

georg
  • 211,518
  • 52
  • 313
  • 390