ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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
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($min = null, $max = null)
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 }
64
68 private function supportsRandomInt()
69 {
70 if (version_compare(PHP_VERSION, '7.0.0', '>=')) {
71 return true;
72 }
73 return false;
74 }
75}
An exception for terminatinating execution or to throw for unit testing.
Component logger with individual log levels by component id.
error($a_message)
logStack($a_level=null, $a_message='')
log stack trace
Wrapper for generation of random numbers, strings, bytes.
logIfPossible(callable $c)
int($min=null, $max=null)
__construct()
ilRandom constructor.
$DIC
Definition: xapitoken.php:46