ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
ilRandom Class Reference

Wrapper for generation of random numbers, strings, bytes. More...

+ Collaboration diagram for ilRandom:

Public Member Functions

 __construct ()
 ilRandom constructor. More...
 
 int ($min=null, $max=null)
 

Private Member Functions

 logIfPossible (callable $c)
 
 supportsRandomInt ()
 

Detailed Description

Wrapper for generation of random numbers, strings, bytes.

Definition at line 7 of file class.ilRandom.php.

Constructor & Destructor Documentation

◆ __construct()

ilRandom::__construct ( )

ilRandom constructor.

Definition at line 13 of file class.ilRandom.php.

14  {
15  }

Member Function Documentation

◆ int()

ilRandom::int (   $min = null,
  $max = null 
)
Parameters
int$min
int$max
Returns
int
Exceptions
Throwable

Definition at line 35 of file class.ilRandom.php.

References Vendor\Package\$e, ilLogLevel\ERROR, ilLogger\error(), logIfPossible(), ilLogger\logStack(), and supportsRandomInt().

36  {
37  if (is_null($min)) {
38  $min = 0;
39  }
40  if (is_null($max)) {
41  $max = mt_getrandmax();
42  }
43 
44  if ($this->supportsRandomInt()) {
45  try {
46  return random_int($min, $max);
47  } catch (Exception $e) {
48  $this->logIfPossible(static function (ilLogger $logger) {
49  $logger->logStack(\ilLogLevel::ERROR);
50  $logger->error('No suitable random number generator found.');
51  });
52  throw $e;
53  } catch (Throwable $e) {
54  $this->logIfPossible(static function (ilLogger $logger) {
55  $logger->logStack(\ilLogLevel::ERROR);
56  $logger->error('max should be greater than min.');
57  });
58  throw $e;
59  }
60  }
61  // version 5.6 => use mt_rand
62  return mt_rand($min, $max);
63  }
logStack($a_level=null, $a_message='')
log stack trace
error($a_message)
logIfPossible(callable $c)
Component logger with individual log levels by component id.
+ Here is the call graph for this function:

◆ logIfPossible()

ilRandom::logIfPossible ( callable  $c)
private
Parameters
callable$c

Definition at line 20 of file class.ilRandom.php.

References Vendor\Package\$c, and $DIC.

Referenced by int().

21  {
22  global $DIC;
23 
24  if (isset($DIC['ilLoggerFactory'])) {
25  $c($DIC->logger()->rnd());
26  }
27  }
$DIC
Definition: xapitoken.php:46
+ Here is the caller graph for this function:

◆ supportsRandomInt()

ilRandom::supportsRandomInt ( )
private
Returns
bool

Definition at line 68 of file class.ilRandom.php.

Referenced by int().

69  {
70  if (version_compare(PHP_VERSION, '7.0.0', '>=')) {
71  return true;
72  }
73  return false;
74  }
+ Here is the caller graph for this function:

The documentation for this class was generated from the following file: