Is it possible for me to derive a function that uses a random integer to derive a pseudorandom number from an indexed list of sequential numbers?

80 Views Asked by At

Assuming I have one random integer, let's say 123456, and an indexed list of numbers [1, 2, 3 ... 100]

Is it possible for me to derive a function that uses the random integer to derive a pseudorandom number from the list?

I would like it to be a function that takes the original index, and returns the remapped/pseudorandom value from the list, e.g.

fn(index) {
    // does something using the list length & random number
    // returns reassigned value
}

conditions:

  1. the function can not do a for-loop over all numbers in the list
  2. if the function is called for each number in the indexed list, the function should never return a duplicate number
  3. only unsigned integers can be used (it's not possible to compute or use decimals or negative numbers where the code runs)
  4. the remapped values should not be sequential relative to the original list of numbers

To try and make it more clear what I am after, here is an example which fulfils condition 1 2, & 3 but fails to fulfil condition 4:

fn(numberFromList) { // any number from [1, 2, 3 ... 100]
    randomOffset = randomInt % listLength;
    if((numberFromList + randomOffset) <= listLength) {
      return index + randomOffset;
    } else {
      return randomOffset - (listLength - numberFromList);
    }
}

Is it possible to fulfil all 4 conditions?

other things which might be helpful:

  • the list of numbers will always start at 1 and increment by 1 in each slot (it could also start at 0 and increment by 1 if that makes this easier)
  • the length of the list of numbers is static and it is known from the start
  • we only ever have one random unsigned integer and must use the same one throughout

This is the closest thing I have ever come across when it comes to the high-level "style" of distribution that I am after, but in my case, the length won't always be a prime number and it may not be viable to compute a prime root of the number: https://youtu.be/YEBfamv-_do?t=308