fork download
  1. <?php
  2.  
  3. // your code goes here
  4. function makeRandomizerWithMemory() {
  5. $memory = array();
  6. $getValue = function($key) use (&$memory) {
  7. if (!array_key_exists($key, $memory)) {
  8. $memory[$key] = mt_rand();
  9. }
  10. return $memory[$key];
  11. };
  12. return function($arg = null) use ($getValue) {
  13. if ($arg) {
  14. $key = strval($arg);
  15. return $getValue($arg);
  16. }
  17. return mt_rand();
  18. };
  19. }
  20.  
  21. $getRand = makeRandomizerWithMemory();
  22.  
  23. $args = array('sobaka', 'kot', 12345, null);
  24.  
  25. foreach ($args as $arg) {
  26. echo "{$arg}: {$getRand($arg)}\n";
  27. echo "{$arg}: {$getRand($arg)}\n\n";
  28. }
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
sobaka: 320760008
sobaka: 320760008

kot: 1623819066
kot: 1623819066

12345: 1127577361
12345: 1127577361

: 1397446740
: 56555524