ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 12 of file class.ilRandom.php.

13  {
14  }

Member Function Documentation

◆ int()

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

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

References ilLogLevel\ERROR, ilLogger\error(), logIfPossible(), ilLogger\logStack(), and supportsRandomInt().

35  {
36  if (is_null($min)) {
37  $min = 0;
38  }
39  if (is_null($max)) {
40  $max = mt_getrandmax();
41  }
42 
43  if ($this->supportsRandomInt()) {
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  // version 5.6 => use mt_rand
61  return mt_rand($min, $max);
62  }
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 19 of file class.ilRandom.php.

References $c, and $DIC.

Referenced by int().

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

◆ supportsRandomInt()

ilRandom::supportsRandomInt ( )
private
Returns
bool

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

Referenced by int().

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

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