ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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.

Referenced by __construct(), and shuffle().

45  {
46  list($usec, $sec) = explode(' ', microtime());
47  return (int)($sec + ($usec * 100000));
48  }
+ 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.

References $seed, and isMtRandomizerAvailable().

Referenced by shuffle().

75  {
76  $seed = (int)$seed; // (mt_)srand seems to not cast to integer itself (string seeds avoid randomizing) !!
77 
78  if( $this->isMtRandomizerAvailable() )
79  {
80  mt_srand($seed);
81  }
82  else
83  {
84  srand($seed);
85  }
86  }
+ 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 124 of file class.ilArrayElementShuffler.php.

Referenced by initSeed(), and shuffleArray().

125  {
126  return function_exists('mt_srand') && function_exists('mt_rand');
127  }
+ Here is the caller graph for this function:

◆ mtShuffle()

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

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

References array.

Referenced by shuffleArray().

108  {
109  $shuffledArray = array();
110 
111  while( count($orderedArray) > 0 )
112  {
113  $key = mt_rand(0, (count($orderedArray)-1));
114  $splice = array_splice($orderedArray, $key, 1);
115  $shuffledArray[] = current($splice);
116  }
117 
118  return $shuffledArray;
119  }
Create styles array
The data for the language used.
+ Here is the caller graph for this function:

◆ setSeed()

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

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

References $seed.

Referenced by __construct().

37  {
38  $this->seed = $seed;
39  }
+ Here is the caller graph for this function:

◆ shuffle()

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

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

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

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

64  {
65  $this->initSeed($this->getSeed());
66  $array = $this->shuffleArray($array);
67  $this->initSeed($this->buildRandomSeed());
68  return $array;
69  }
+ 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 92 of file class.ilArrayElementShuffler.php.

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

Referenced by shuffle().

93  {
94  if( $this->isMtRandomizerAvailable() )
95  {
96  return $this->mtShuffle($array);
97  }
98 
99  shuffle($array);
100  return $array;
101  }
+ 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: