Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

115
Visualizações
Generate same random number based on string parameter

I'm trying to create a function that will generate the same random number on each execution if the string is the same.

function generateRandom(maxInt, stringParam) {
  //generate random based on stringParam
  //output int number from 0 to maxInt
}

The goal is to generate the same random number (on different pages) if the stringKey is the same.

generateRandom(100, 'abc') // 73
generateRandom(100, 'abc') // 73
generateRandom(100, 'abc') // 73

generateRandom(100, 'qwe') // 45
generateRandom(100, 'qwe') // 45
generateRandom(100, 'qwe') // 45
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

This isn't really random. I would say this is a kind of encryption. Hopefully, it works for your requirement.

First, get the Unicode from each character in the string. Sum them up and then use % (Remainer operator) to round the number to be within MaxInt range. This will result in the same number every time you input the same maxInt and the same stringParam.

function generateRandom(maxInt, stringParam) {
  let sum = 0;
  for (let i = 0; i < stringParam.length; i++){
    sum += stringParam.charCodeAt(i);
  }
  let result = sum % maxInt;
  console.log(result);
  return result;
}

generateRandom(100, 'abc')
generateRandom(100, 'abc')
generateRandom(100, 'abc')

generateRandom(100, 'qwe')
generateRandom(100, 'qwe')
generateRandom(100, 'qwe')

about 4 years ago · Juan Pablo Isaza Relatório

0

I'd suggest using a hashing function for this purpose, perhaps a simple one in this case.

Once we have the hash we'll mod with our max output value, e.g. hash % maxInt.

This will give the same output for a given input.

// Simple implementation of a string hashing function (Bernstein)
function hashCode(input) {
    return [...input].reduce((hash, chr) => { 
        return hash * 33 + chr.charCodeAt(0);
    }, 0);
}

function generateRandom(maxInt, stringParam) {
    return hashCode(stringParam) % maxInt;
}

const maxInt = 100;
const inputs = [ 'qwe', 'qwe', 'abc', 'abc', 'def', 'def'];

console.log('In ', 'Out')
inputs.forEach(input => console.log(input, generateRandom(maxInt, input)));
.as-console-wrapper { max-height: 100% !important; }

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda