ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
ilArrayElementShuffler Class Reference
+ Inheritance diagram for ilArrayElementShuffler:
+ Collaboration diagram for ilArrayElementShuffler:

Public Member Functions

 __construct ()
 
 getSeed ()
 
 setSeed ($seed)
 
 buildRandomSeed ()
 
 buildSeedFromString ($string)
 
 shuffle ($array)
 

Protected Attributes

 $seed
 

Private Member Functions

 initSeed ($seed)
 
 shuffleArray ($array)
 
 mtShuffle ($orderedArray)
 
 isMtRandomizerAvailable ()
 

Detailed Description

Definition at line 11 of file class.ilArrayElementShuffler.php.

Constructor & Destructor Documentation

◆ __construct()

ilArrayElementShuffler::__construct ( )

Definition at line 20 of file class.ilArrayElementShuffler.php.

References buildRandomSeed(), and setSeed().

+ Here is the call graph for this function:

Member Function Documentation

◆ buildRandomSeed()

ilArrayElementShuffler::buildRandomSeed ( )
Returns
integer

Definition at line 44 of file class.ilArrayElementShuffler.php.

45 {
46 list($usec, $sec) = explode(' ', microtime());
47 return (int) ($sec + ($usec * 100000));
48 }

Referenced by __construct(), and shuffle().

+ Here is the caller graph for this function:

◆ buildSeedFromString()

ilArrayElementShuffler::buildSeedFromString (   $string)
Parameters
string$string
Returns
integer

Definition at line 54 of file class.ilArrayElementShuffler.php.

55 {
56 return hexdec(substr(md5($string), 0, 10));
57 }

◆ getSeed()

ilArrayElementShuffler::getSeed ( )
Returns
integer

Definition at line 28 of file class.ilArrayElementShuffler.php.

References $seed.

Referenced by shuffle().

+ Here is the caller graph for this function:

◆ initSeed()

ilArrayElementShuffler::initSeed (   $seed)
private
Parameters
integer$seed

Definition at line 74 of file class.ilArrayElementShuffler.php.

75 {
76 $seed = (int) $seed; // (mt_)srand seems to not cast to integer itself (string seeds avoid randomizing) !!
77
78 if ($this->isMtRandomizerAvailable()) {
79 mt_srand($seed);
80 } else {
81 srand($seed);
82 }
83 }

References $seed, and isMtRandomizerAvailable().

Referenced by shuffle().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isMtRandomizerAvailable()

ilArrayElementShuffler::isMtRandomizerAvailable ( )
private
Returns
bool

Definition at line 119 of file class.ilArrayElementShuffler.php.

120 {
121 return function_exists('mt_srand') && function_exists('mt_rand');
122 }

Referenced by initSeed(), and shuffleArray().

+ Here is the caller graph for this function:

◆ mtShuffle()

ilArrayElementShuffler::mtShuffle (   $orderedArray)
private
Parameters
array$orderedArray
Returns
array

Definition at line 103 of file class.ilArrayElementShuffler.php.

104 {
105 $shuffledArray = array();
106
107 while (count($orderedArray) > 0) {
108 $key = mt_rand(0, (count($orderedArray) - 1));
109 $splice = array_splice($orderedArray, $key, 1);
110 $shuffledArray[] = current($splice);
111 }
112
113 return $shuffledArray;
114 }

Referenced by shuffleArray().

+ Here is the caller graph for this function:

◆ setSeed()

ilArrayElementShuffler::setSeed (   $seed)
Parameters
integer$seed

Definition at line 36 of file class.ilArrayElementShuffler.php.

37 {
38 $this->seed = $seed;
39 }

References $seed.

Referenced by __construct().

+ Here is the caller graph for this function:

◆ shuffle()

ilArrayElementShuffler::shuffle (   $array)
Parameters
array$array
Returns
array

Reimplemented in ilArrayElementOrderKeeper.

Definition at line 63 of file class.ilArrayElementShuffler.php.

64 {
65 $this->initSeed($this->getSeed());
66 $array = $this->shuffleArray($array);
67 $this->initSeed($this->buildRandomSeed());
68 return $array;
69 }

References buildRandomSeed(), getSeed(), initSeed(), and shuffleArray().

Referenced by assClozeGap\getItems(), and shuffleArray().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ shuffleArray()

ilArrayElementShuffler::shuffleArray (   $array)
private
Parameters
array$array
Returns
array

Definition at line 89 of file class.ilArrayElementShuffler.php.

90 {
91 if ($this->isMtRandomizerAvailable()) {
92 return $this->mtShuffle($array);
93 }
94
95 shuffle($array);
96 return $array;
97 }

References isMtRandomizerAvailable(), mtShuffle(), and shuffle().

Referenced by shuffle().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $seed

ilArrayElementShuffler::$seed
protected

Definition at line 16 of file class.ilArrayElementShuffler.php.

Referenced by getSeed(), initSeed(), and setSeed().


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