Transforming numbers into fake randoms that are actually deterministic

34 Views Asked by At

How can I convert a number into a seemingly random number in a process that is actually deterministic?

For example I'd like to transform: 123456 into 51. Or 28. Doesn't matter as long as it's a number between 1 - 1000.

The value should be significantly different for similar inputs. Transforming 123456 should give a very different number from transforming 123457. I recall reading about these processes when I was younger in some paper about cryptography but i can't recall their name. "Cascading functions?" "Waterfall functions?" I don't remember but anyway.. I was talking about the avalanche effect.

The process should be deterministic. It should give the same output for input 123456 every time I run it.

Context:

I'm creating a tool that scours logfiles across geographical servers and helps us view the errors and their frequency so we can understand cause-and-effect. Each error occurrence appears at a point on a time plot.

The x-axis has the time of the error. The y-axis; I don't need to use. But I don't want all the errors that happened at a similar time to appear on the same y-point on my plot.

So what I'm doing is the following:

I select a random number for that log's y-axis value. But when I run my tool again, that point appears on a different position because of the random number. It's no real problem for the investigative purposes we use it for but it looks a bit wonky, every time I run the tool I don't want the point to jump around.

Image of the tool

So instead of picking a random number, I've thought of chopping the log id (given as i.e '94827717') into a 1-10 decimal, like so:

  // this abomination actually works but its "randomness" probably 
  // depends on the number of concurrent users using the log service. 
  // I suspect `id` is an autoincrement value
  hashLogIdToNumber: id => {
    const wholeNumber = id.charAt(len - 3)
    const firstDecimal = id.charAt(len - 2)
    const secondDecimal = id.charAt(len - 1)

    return parseFloat(`${wholeNumber}.${firstDecimal}${secondDecimal}`)
  }

I use the last parts of the log id because they are the ones with the most variation. I suspect that log.id is an autoincremented value. I don't have control over it, it's given by the log service provider.

1

There are 1 best solutions below

1
On BEST ANSWER

Run a random number generator with the input number as the seed.