1

Is is possible to generate a pseudo-random number based on a string, meaning that a specific string would ALWAYS return the same number?

Tom
  • 1,217
  • 3
  • 12
  • 15
  • 2
    Well then it wouldn't be random would it? and, yes, it is possible. you should amend your question to ask how it would be done and narrow down to a specific scope rather than just asking if it is possible. – Kevin B Sep 18 '14 at 18:09
  • 2
    Are you looking for a hashing function? (And if so, do you want one to use in Javascript, or PHP? Your question is tagged with both...) But, possibly more importantly, why do you want to do this? It's important to choose the right hashing function for your usage. – Matt Gibson Sep 18 '14 at 18:10
  • I need to do this in PHP, sorry for tagging both, my bad. I just want to assign a "random" color to each file extension. For example, ".jpg" should always be assigned the same random color. I don't know if I'm making sense or not. – Tom Sep 18 '14 at 18:19
  • And what do you want as output, a colour code? Something as simple as `bin2hex(substr($filename, -3, 3));` (or only a little more complicated) will likely give you a unique HTML colour code given a `$filename` with a three-character extension, but the colours are likely to be from a relatively narrow range. You may need to clarify what kind of output you want, whether it's okay for the colours to be close together, etc. It's possible that you should just prepare a table of colours and assign file extensions to them as you find them... – Matt Gibson Sep 18 '14 at 21:07

2 Answers2

2

What you need is not a random, but a hashing function.

A hashing algorithm creates a fingerprint of input data. The hash is usually not reversible, i.e. you cannot reconstruct the data from the hash. (However, if the algorithm is flawed, you can generate collisions, but as long as you're not doing anything security related, this is nothing to worry about.)

Unfortunately, there are no native hashing algoritms implemented in JS, so you must implement your own. However here's a SO post that shows an implementation: https://stackoverflow.com/a/7616484/3908235

And, apparently there is a whole library with the implementation of common algorithms (thanks @MattGibson).

Community
  • 1
  • 1
lxg
  • 12,375
  • 12
  • 51
  • 73
0

If you want to do this in PHP, then you could use the crc32. It's not random but it produces numbers that will vary based on the contents of the string. It's not 100% guaranteed that two different strings will produce different numbers.

Example:

$number = crc32("http://php.net/manual/en/function.crc32.php");

Note that $number will be an unsigned 32bit integer, but PHP interprets all integers as signed integers, the value may be represented differently between 32bit environments - which may return negative and positive numbers - and 64bit environments - which will return only positive numbers.

It's a good idea to format the output using printf to output this to your html,

printf("%u", $number);

To make comparisons of values generated across different architectures, you should also format the string using sprintf, just to be safe:

$numberToStoreShareOrCompare = sprintf("%u", $number);
0x00h
  • 760
  • 5
  • 12