Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Random.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Framework\Math;
7 
10 
17 class Random
18 {
22  const CHARS_LOWERS = 'abcdefghijklmnopqrstuvwxyz';
23 
24  const CHARS_UPPERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
25 
26  const CHARS_DIGITS = '0123456789';
27 
39  public function getRandomString($length, $chars = null)
40  {
41  $str = '';
42  if (null === $chars) {
43  $chars = self::CHARS_LOWERS.self::CHARS_UPPERS.self::CHARS_DIGITS;
44  }
45 
46  $charsMaxKey = mb_strlen($chars) - 1;
47  for ($i = 0; $i < $length; $i++) {
48  $str .= $chars[self::getRandomNumber(0, $charsMaxKey)];
49  }
50 
51  return $str;
52  }
53 
62  public static function getRandomNumber($min = 0, $max = null)
63  {
64  if (null === $max) {
65  $max = mt_getrandmax();
66  }
67 
68  if ($max < $min) {
69  throw new LocalizedException(new Phrase('Invalid range given.'));
70  }
71 
72  return random_int($min, $max);
73  }
74 
82  public function getUniqueHash($prefix = '')
83  {
84  return $prefix . $this->getRandomString(32);
85  }
86 }
static getRandomNumber($min=0, $max=null)
Definition: Random.php:62
$prefix
Definition: name.phtml:25
getRandomString($length, $chars=null)
Definition: Random.php:39
$i
Definition: gallery.phtml:31