Question: I have many strings. Each string something like:

* "i\_love\_pizza\_123"
* "whatever\_this\_is\_now\_later"
* "programming\_is\_awesome"
* "stack\_overflow\_ftw"
* ...etc

I need to be able to convert each string to a random number, 1-10. Each time that string gets converted, it should consistently be the same number. A sampling of strings, even with similar text should result in a fairly even spread of values 1-10.

My first thought was to do something like `md5($string)`, then break down a-f,0-9 into ten roughly-equal groups, determine where the first character of the hash falls, and put it in that group. But doing so seems to have issues when converting 16 down to 10 by multiplying by 0.625, but that causes the spread to be uneven.

Thoughts on a good method to consistently convert a string to a random/repeatable number, 1-10? There has to be an easier way.

Answer: 
Here's a quick demo how you can do it.

```
function getOneToTenHash($str) {
    $hash = hash('sha256', $str, true);
    $unpacked = unpack("L", $hash); // convert first 4 bytes of hash to 32-bit unsigned int
    $val = $unpacked[1];

    return ($val % 10) + 1; // get 1 - 10 value
}

for ($i = 0; $i < 100; $i++) {
    echo getOneToTenHash('str' . $i) . "\n";
}

```

**How it works**:

Basically you get the output of a hash function and downscale it to desired range (1..10 in this case).

In the example above, I used `sha256` hash function which returns 32 bytes of arbitrary binary data. Then I extract just first 4 bytes as integer value (`unpack()`).
At this point I have a 4 bytes integer value (0..4294967295 range). In order to downscale it to 1..10 range I just take the remainder of division by 10 (0..9) and add 1.
It's not the only way to downscale the range but an easy one.

So, the above example consists of 3 steps:

1. get the hash value
2. convert the hash value to integer
3. downscale integer range

A much shorter example with `crc32()` function which returns integer value right away thus allowing us to omit step 2:

```
function getOneToTenHash($str) {
    $int = crc32($str); // 0..4294967295
    return ($int % 10) + 1; // 1..10
}

```
below maybe what u want

```php
$inStr = "hello world";
$md5Str = md5($inStr);

$len = strlen($md5Str);
$out = 0;
for($i=0; $i<$len; $i++) {
    $out = 7*$out + intval($md5Str[$i]); // if you want more random, can and random() here
}

$out = ($out % 10 + 9)%10; // scope= [1,10]

```