Question

How would I take a textual input from the user (anything their keyboard would allow them to type), and transfer it to a number?

From there, I would probably take that number, and feed it into a seeded random number generator.

I'm getting the idea from Minecraft's random seed option, but I can't find anything on it.

Was it helpful?

Solution

There are probably more interesting algorithms but this just converts the characters to ints and multiplies it by the position in order to weight them (so that 'abc' is different than 'cba'). You could also use a hash function of some kind as well, but I thought that might be overkill for this purpose. ​

var input = 'askljfhasjfh', num = 0;

for (var i = 0, len = input.length; i < len; ++i) {
  num += input.charCodeAt(i) * (i + 1);
}

console.log(num);

Keep in mind though, you can't seed the Math.random() function in Javascript, it always just uses the current date for the seed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top