0

It is possible to use a string as a randomiser seed to generate a number between two values in PHP.

For example:

$seed = 'John';

srand($seed);

echo rand(1, 10);
workingPlock
  • 85
  • 1
  • 8

1 Answers1

1

You can calculate the hash of the input string to get an integer (with crc32() in example) and then, use that number as seed :

<?php
$StringSeed = "John";
$IntSeed = crc32($StringSeed);

echo "Hash Value : $IntSeed" . PHP_EOL;

srand($IntSeed);

echo rand(1, 10); // 8
echo rand(1, 10); // 4
echo rand(1, 10); // 10

Try it yourself

Cid
  • 14,968
  • 4
  • 30
  • 45