ILIAS  release_7 Revision v7.30-3-g800a261c036
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(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}
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.
int(int $min=null, int $max=null)
logIfPossible(callable $c)
__construct()
ilRandom constructor.
$c
Definition: cli.php:37
global $DIC
Definition: goto.php:24