4

Generate random 6 characters based on input. Like I want to turn 1028797107357892628 into j4w8p. Or 102879708974181177 into lg36k but I want it to be consistant. Like whenever I feed 1028797107357892628 in, it should always spit out j4w8p. Is this possible? (Without a database if possible.) I know how to generate random 6 characters but I dont know how to connect it with an input tbh. I would appreciate any help, thanks.

let rid = (Math.random() + 1).toString(36).substring(7); 
Hasan Kayra
  • 101
  • 1
  • 6
  • You're probably looking for a "seed". These parameters are inputted into functions and decide what they output -- for example, a unix timestamp can be used as a seed. https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript may be able to help you with this. – LeoDog896 Oct 09 '22 at 22:52
  • 5
    It's called hashing usually – Konrad Oct 09 '22 at 22:53
  • 1
    it's called a PRNG, there's lots of them, some quite short in code length if needed. The built-in crypto hashing can also be used by chopping off 64 bit chunks, converting to an int, then calling `num.toString(36)` to get your alphabetical representation. – dandavis Oct 09 '22 at 23:02
  • PRNG = pseudorandom number generator – Yogi Oct 09 '22 at 23:07
  • Note that the Javascript [Number object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) has 15 - 17 digits of precision, and therefore treating `1028797107357892628` as a Number rather than String or BigInt results in a rounded value of `1028797107357892600`. – Trentium Oct 10 '22 at 01:45

3 Answers3

2

You can create a custom hashing function a simple function to your code would be

const seed = "1028797089741811773";

function customHash(str, outLen){
  //The 4 in the next regex needs to be the length of the seed divided by the desired hash lenght
  const regx = new RegExp(`.{1,${Math.floor(str.length / outLen)}}`, 'g')
  const splitted = str.match(regx);
  
 
  
  let out = "";
  for(const c of splitted){
    let ASCII = c % 126;
    if( ASCII < 33) ASCII = 33
  
   out += String.fromCharCode(ASCII)
  }

  return out.slice(0, outLen)
}


const output = customHash(seed, 6)

console.log(output)
Ahmed Gaafer
  • 1,603
  • 9
  • 26
0

It is called hashing, hashing is not random. In your example to get rid:

let rid = (Math.random() + 1).toString(36).substring(7);

Because it is random, it's impossible to be able to produce "consistant result" as you expect.

You need algorithm to produce a "random" consistant result.

thebluetropics
  • 82
  • 2
  • 10
0

Thanks everyone, solved my issue.

Code:

  let seed = Number(1028797089741811773)
  let rid = seed.toString(36).substring(0,6)
  console.log(rid)

Or:

  let seed = Number(1028797089741811773)
  let rid = seed.toString(36).substring(6)
  console.log(rid)
Hasan Kayra
  • 101
  • 1
  • 6
  • The output is not 6 characters. Would .substring(0,6) work better? – Yogi Oct 09 '22 at 23:38
  • Yeah i noticed, but its not that important. I just estimated 6 would be nice, but 5 works too. But you are right, would be better like that. – Hasan Kayra Oct 09 '22 at 23:48
  • Yet it is important as you specifically asked for 6 characters in the question. – Yogi Oct 10 '22 at 05:51