ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilRandom.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
7 class ilRandom
8 {
9 
13  public function __construct()
14  {
15  }
16 
20  private function logIfPossible(callable $c)
21  {
22  global $DIC;
23 
24  if (isset($DIC['ilLoggerFactory'])) {
25  $c($DIC->logger()->rnd());
26  }
27  }
28 
35  public function int(int $min = null, int $max = null) :int
36  {
37  if (is_null($min)) {
38  $min = 0;
39  }
40  if (is_null($max)) {
41  $max = mt_getrandmax();
42  }
43 
44  try {
45  return random_int($min, $max);
46  } catch (Exception $e) {
47  $this->logIfPossible(static function (ilLogger $logger) {
48  $logger->logStack(\ilLogLevel::ERROR);
49  $logger->error('No suitable random number generator found.');
50  });
51  throw $e;
52  } catch (Throwable $e) {
53  $this->logIfPossible(static function (ilLogger $logger) {
54  $logger->logStack(\ilLogLevel::ERROR);
55  $logger->error('max should be greater than min.');
56  });
57  throw $e;
58  }
59  }
60 }
__construct()
ilRandom constructor.
logStack($a_level=null, $a_message='')
log stack trace
$c
Definition: cli.php:37
int(int $min=null, int $max=null)
error($a_message)
logIfPossible(callable $c)
global $DIC
Definition: goto.php:24
Wrapper for generation of random numbers, strings, bytes.
Component logger with individual log levels by component id.