Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
randomsequence.php
1<?php
2namespace Bitrix\Main\Type;
14{
15 private $mz = 0;
16 private $mw = 0;
17
24 public function __construct($seed = "")
25 {
26 $md = md5($seed);
27 $this->mz = crc32(mb_substr($md, 0, 16));
28 $this->mw = crc32(mb_substr($md, -16));
29 }
30
37 public function getNext()
38 {
39 $this->mz = 36969 * ($this->mz & 65535) + ($this->mz >> 16);
40 if($this->mz > 0x7FFFFFFF)
41 $this->mz = -(0xFFFFFFFF - $this->mz + 1);
42
43 $this->mw = 18000 * ($this->mw & 65535) + ($this->mw >> 16);
44 if($this->mw > 0x7FFFFFFF)
45 $this->mw = -(0xFFFFFFFF - $this->mw + 1);
46
47 //return ($this->mz << 16) + $this->mw;
48
49 $r = ($this->mz << 16) & 0xFFFF0000;
50 if($r > 0x7FFFFFFF)
51 $r = -(0xFFFFFFFF - $r + 1);
52
53 $r += $this->mw;
54 if($r > 0x7FFFFFFF)
55 $r = -(0xFFFFFFFF - $r + 1);
56
57 return ($r & 0xFFFFFFFF);
58 }
59
69 public function rand($min, $max)
70 {
71 if ($min >= $max)
72 throw new \Bitrix\Main\NotSupportedException("max parameter must be greater than min.");
73
74 $r = sprintf("%u", $this->getNext()) / sprintf("%u", 0xffffffff);
75 return intval($min + $r * ($max - $min + 1));
76 }
77
84 public function randString($length = 10)
85 {
86 static $allChars = "abcdefghijklnmopqrstuvwxyzABCDEFGHIJKLNMOPQRSTUVWXYZ0123456789";
87 $result = "";
88 for ($i = 0; $i < $length; $i++)
89 {
90 $result .= $allChars[$this->rand(0, 61)];
91 }
92 return $result;
93 }
94}