ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 ()
 

Private Attributes

 $logger = null
 

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 17 of file class.ilRandom.php.

18  {
19  }

Member Function Documentation

◆ int()

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

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

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

40  {
41  if(is_null($min)) {
42  $min = 0;
43  }
44  if(is_null($max)) {
45  $max = mt_getrandmax();
46  }
47 
48  if($this->supportsRandomInt()) {
49  try {
50  return random_int($min, $max);
51  } catch (Exception $e) {
52  $this->logIfPossible(static function (ilLogger $logger) {
53  $logger->logStack(\ilLogLevel::ERROR);
54  $logger->error('No suitable random number generator found.');
55  });
56  throw $e;
57  } catch (Throwable $e) {
58  $this->logIfPossible(static function (ilLogger $logger) {
59  $logger->logStack(\ilLogLevel::ERROR);
60  $logger->error('max should be greater than min.');
61  });
62  throw $e;
63  }
64  }
65  // version 5.6 => use mt_rand
66  return mt_rand($min, $max);
67  }
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 24 of file class.ilRandom.php.

References $DIC.

Referenced by int().

25  {
26  global $DIC;
27 
28  if (isset($DIC['ilLoggerFactory'])) {
29  $c($DIC->logger()->rnd());
30  }
31  }
global $DIC
Definition: saml.php:7
+ Here is the caller graph for this function:

◆ supportsRandomInt()

ilRandom::supportsRandomInt ( )
private
Returns
bool

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

Referenced by int().

73  {
74  if(version_compare(PHP_VERSION , '7.0.0', '>=')) {
75  return true;
76  }
77  return false;
78  }
+ Here is the caller graph for this function:

Field Documentation

◆ $logger

ilRandom::$logger = null
private

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

Referenced by int().


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