How can I make the php shuffle function use a seed, so that when I use the same seed, the shuffle function will output the same array. I read that shuffle is automatically seeded. Is there a way to get the seed of that shuffle used, or how can I create/mimic shuffle with a custom seed?
You can't retrieve the seed used by shuffle, but you can simulate shuffle and fix your own seed:
$array = range(1, 10);
function seededShuffle(array &$array, $seed) {
mt_srand($seed);
$size = count($array);
for ($i = 0; $i < $size; ++$i) {
list($chunk) = array_splice($array, mt_rand(0, $size-1), 1);
array_push($array, $chunk);
}
}
$seed = date('Ymd');
seededShuffle($array, $seed);
var_dump($array);
This will set a different seed each day, but throughout the day it will use the same seed and shuffle the array in the same order; tomorrow will be a different random shuffle to today
For today (6th June 2015), the sequence should be
3, 6, 9, 2, 7, 1, 8, 5, 10, 4
PHP does not have shuffling with seeding, but you can do this instead:
$an_array = array('a','b','c','d');
$indices = array(0,1,2,3);
// shuffle the indices and use them as shuffling seed
shuffle($indices);
// then whenever you want to produce exactly same shuffle use the pre-computed shuffled indices
function shuffle_precomputed($a, $shuffled_indices)
{
$b = $a; // copy array
foreach ($shuffled_indices as $i1=>$i2) $a[$i2] = $b[$i1];
return $a;
}
use like this:
$shuffled_array = shuffle_precomputed($an_array, $indices);
You can even use the factoradic number system to transform the $shuffled_indices array to/from a unique integer number that can be used as a unique seed, then simply compute the shuffle from the factoradic number to be used in shuffle_precomputed function.
For additional shuffle variations for PHP you may want to see:
PHP - shuffle only part of an array
Efficiently pick n random elements from PHP array (without shuffle)
Related
Let me ask you how to return any three random numbers into the array that is different from the other numbers in the array.
$checkrandom=array(1,2,3,4);
for($i=0;$i<3;$i++){
$random=RAND(1,10);
if(!in_array($random,$checkrandom)){
array_push($checkrandom,$random);
}else{
//do something
}
}
Rather than guess numbers and add them if they don't already exist, this creates an array of the remaining numbers (using a combination of range() and array_diff()). It then picks numbers out of that array (using shuffle() and array_shift()) and adds them to the end of $checkrandom.
$checkrandom=array(1,2,3,4);
$numbers = array_diff(range(1,10), $checkrandom);
for($i=0;$i<3;$i++){
shuffle($numbers);
$checkrandom[]=array_shift($numbers);
}
print_r($checkrandom);
Or, as deceze pointed out, just shuffle the remaining array and use array_slice() to extract the first 3 numbers...
$checkrandom=array(1,2,3,4);
$numbers = array_diff(range(1,10), $checkrandom);
shuffle($numbers);
$checkrandom = array_merge($checkrandom, array_slice($numbers, 0, 3));
print_r($checkrandom);
I'm trying to create a random number generator in PHP. It's supposed to generate three (3) numbers at a time, without repeat. That's to say, the 3 numbers cannot be the same.
Here's what I've tried so far:
$array = [];
$A = mt_rand(1,36);
$array[0] = $A;
$B = mt_rand(1,36);
$array[1] = $B;
if(in_array($B,$array)){
$B = mt_rand(1,36);
$array[1] = $B;
}
$C = mt_rand(1,36);
$array[2] = $C;
if(in_array($C,$array)){
$C = mt_rand(1,36);
$array[2] = $C;
}
$length = count($array);
//display the array values;
for($i = 0; $i < $length; $i++){
echo ($array[$i]."<br>");
}
Can anyone tell me where I'm going wrong?
Like this ( as per my initial comment ),
$array = [];
while( count($array) < 3 ){
$rand = mt_rand(1,36);
$array[$rand] = $rand;
}
print_r( $array );
By setting the "key" to be the random number, we can abuse the fact that associative array keys are unique. Then it's a simple matter of waiting until the array contains the desired amount of unique items.
You can test it here
Outputs: ( your results may vary, it's random )
Array
(
[16] => 16
[20] => 20
[27] => 27
)
UPDATE I was trying to think of a valid way to do it without using a loop ( on my way home from work ), and this way may be even better in some cases.
$a = range(1,36);
shuffle($a);
$array = array_slice($a, 0, 3);
print_r($array);
This will have better performance when the number of items you must find is higher. This is because there is no repetition, no collisions. So if you have a small range but need to find many items for the return, this will preform better. If you have many items and need to return only few, then the first one may be better, if not from speed then from memory use.
You can see it here
For reference this uses
range() - Create an array containing a range of elements.
http://php.net/manual/en/function.range.php
shuffle() - Shuffles (randomizes the order of the elements in) an array. It uses a pseudo random number generator that is not suitable for cryptographic purposes.
http://php.net/manual/en/function.shuffle.php
array_slice() - Returns the sequence of elements from the array as specified by the offset and length parameters.
http://php.net/manual/en/function.array-slice.php
So to explain this last one
First we create an array that contains each of our possible numbers as an element. So for example like this [1,2,3,4,5,6, ...].
Next we shuffle it which randomizes the order of the whole array. Shuffle modifies the array by "reference" so it doesn't return our array and therefor there is no assignment ( I think it returns Boolean, however I'm at a loss as to how it could fail and return false, pretty much it just returns true which we don't want to overwrite our array with ). So our example then becomes this [16,20,27,14,5,1, ...]
Last we cut out the number of items we need to return. Finally we end the example with this [16,20,27];
You can crunch the first one down into one ( really 2) line by assigning the value of the $rand variable in the condition of the loop. Like this:
$array = [];
while( count($array) < 3 && false !== ($rand = mt_rand(1,36))) $array[$rand] = $rand;
Because mt_rand(1,36) will never return boolan false. Also if I remember mt_rand is the same as rand now, or at least in current PHP versions.
Note: As of PHP 7.1.0, rand() uses the same random number generator as mt_rand(). To preserve backwards compatibility rand() allows max to be smaller than min as opposed to returning FALSE as mt_rand(). http://php.net/manual/en/function.rand.php
Hope it helps you, remember to think outside of the box.
the roadblock:
using multiple nested arrays and wanting to select the array with the largest int in a specific position; in the selected array change another specific position's value.
psuedo code might look like this:
$array1 = array(2, 23, 7);
$array2 = array(2, 21, 7);
$Mutt = array($L, $P, $O, $array1)
$Jeff = array($L, $P, $O, $array2)
find array with max [3][1] {
('selected' [1]++)
}
in real life this might look like:
Mutt ($Mutt) has the worked the most days this month ($Mutt [3][1]),
he has earned an additional 'personal day' ($Mutt [1]).
I have tried to find a solution to this. I may be too new to understand how to word my search correctly, but I am having no luck.
I hope I have undestood well what you want:
Your arrays:
$array1 = array(2, 23, 7);
$array2 = array(2, 21, 7);
$Mutt = array($L, $P, $O, $array1);
$Jeff = array($L, $P, $O, $array2);
Creating a new array $people which contains $Mutt and $Jeff (passed by reference).
$people=array(&$Mutt,&$Jeff);
Creating function findMaxIndex, which returns the index for which $people[that index] is the array with maximum value at the position that we want.
Its arguments are:
$arr, the array which contains the arrays we want to compare (in this case, $people)
$pos1 and $pos2, which are the indexes we want to compare
So... we will compare
$arr[0][$pos1][$pos2]
$arr[1][$pos1][$pos2]
...
$arr[count($arr)-1][$pos1][$pos2]
This function works like this:
It creates the array $max, where $max[0] is the index of $arr
with the maximum value (among the arrays we have examined until that
moment), and $max[1] is that value.
It iterates through all $arr
If it finds that current value ($arr[$i][$pos1][$pos2]) is greater
than the maximum value, $max is updated and becomes
array($i,$arr[$i][$pos1][$pos2]).
Finally, it returns $max[0], which is the index for which
$people[that index] is the array with maximum value at the
position that we want.
The function is:
function findMaxIndex($arr,$pos1,$pos2){
$max=array(0,$arr[0][$pos1][$pos2]);
for($i=1;$i<count($arr);$i++){
if($arr[$i][$pos1][$pos2]>$max[1]){
$max=array($i,$arr[$i][$pos1][$pos2]);
}
}
return $max[0];
}
Then we call the function...
$maxIndex=findMaxIndex($people,3,1);
... which gives 0, so the array with maximum value is $people[0] ($Mutt)
Finally, we increase that array:
$people[$maxIndex][1]++;
$Mutt and $Jeff are modified too because we passed them by reference.
In short,
$array1 = array(2, 23, 7);
$array2 = array(2, 21, 7);
$Mutt = array($L, $P, $O, $array1);
$Jeff = array($L, $P, $O, $array2);
$people=array(&$Mutt,&$Jeff);
function findMaxIndex($arr,$pos1,$pos2){
$max=array(0,$arr[0][$pos1][$pos2]);
for($i=1;$i<count($arr);$i++){
if($arr[$i][$pos1][$pos2]>$max[1]){
$max=array($i,$arr[$i][$pos1][$pos2]);
}
}
return $max[0];
}
$maxIndex=findMaxIndex($people,3,1);//gives `0` -> Max is `$people[0]`
$people[$maxIndex][1]++;
==============================================
And if you want multiple indexes in case of tie (changes in bold):
Your arrays:
$array1 = array(2, 23, 7);
$array2 = array(2, 21, 7);
$Mutt = array($L, $P, $O, $array1);
$Jeff = array($L, $P, $O, $array2);
Creating a new array $people which contains $Mutt and $Jeff
$people=array(&$Mutt,&$Jeff);
Creating function findMaxIndex, which returns the array which contains the indexes for which $people[that index] is the array with maximum value at the position that we want.
Its arguments are:
$arr, the array which contains the arrays we want to compare (in this case, $people)
$pos1 and $pos2, which are the indexes we want to compare
So... we will compare
$arr[0][$pos1][$pos2]
$arr[1][$pos1][$pos2]
...
$arr[count($arr)-1][$pos1][$pos2]
This function works like this:
It creates the array $max, where $max[0] is an array which contains the indexes of $arr
with the maximum value (among the arrays we have examined until that
moment), and $max[1] is that value.
It iterates through all $arr
If it finds that current value ($arr[$i][$pos1][$pos2]) is greater
than the maximum value, $max is updated and becomes
array(array($i),$arr[$i][$pos1][$pos2]).
If not, and if it finds that current value ($arr[$i][$pos1][$pos2]) equals the maximum value, $max[0] is updated and $i is pushed into it.
Finally, it returns $max[0], which is the array which contains the indexes for which
$people[that index] is the array with maximum value at the
position that we want.
The function is:
function findMaxIndex($arr,$pos1,$pos2){
$max=array(array(0),$arr[0][$pos1][$pos2]);
for($i=1;$i<count($arr);$i++){
$current=$arr[$i][$pos1][$pos2];
if($current>$max[1]){
$max=array(array($i),$current);
}else if($current==$max[1]){
array_push($max[0],$i);
}
}
return $max[0];
}
Then we call the function...
$maxIndex=findMaxIndex($people,3,0);
... which gives array(0,1), so the arrays with maximum value are $people[0] ($Mutt) and $people[1] ($Jeff).
Finally, we increase the arrays:
for($i=0;$i<count($maxIndex);$i++){
$people[$maxIndex[$i]][1]++;
}
$Mutt and $Jeff are modified too because we passed them by reference.
In short,
$array1 = array(2, 23, 7);
$array2 = array(2, 21, 7);
$Mutt = array($L, $P, $O, $array1);
$Jeff = array($L, $P, $O, $array2);
$people=array(&$Mutt,&$Jeff);
function findMaxIndex($arr,$pos1,$pos2){
$max=array(array(0),$arr[0][$pos1][$pos2]);
for($i=1;$i<count($arr);$i++){
$current=$arr[$i][$pos1][$pos2];
if($current>$max[1]){
$max=array(array($i),$current);
}else if($current==$max[1]){
array_push($max[0],$i);
}
}
return $max[0];
}
$maxIndex=findMaxIndex($people,3,0);//gives `array(0,1)` -> Tie between `$people[0]` and `$people[1]`
for($i=0;$i<count($maxIndex);$i++){
$people[$maxIndex[$i]][1]++;
}
I’m not sure how important your max value is to the overall problem. This looks kind of messy. I think of 3 different ways to handle this.
Create and addition array that hold and updates the max value each time the arrayt is mutated. This is quick dirty and not a great solution but depending on over value of the project might be ok.
Write a function to search for the highest value, excessive but will work.
A little more advance but probably the most professional solution, redesign your program using classes. Each array will now become an object of its own. Each object can track its own max value in class variable with its mutator or add function updating it as you go. Now you can simply have one array made up of these objects and that array can be sorted easily (sort by the max value of each object using an accessor for that value) then just pull the object from the top or bottom of the array depending on which way you sorted it. You will probably want to write a private search function on the class level to find the greatest value on creation.
Does anyone know what's the randomness of PHP's shuffle() function? Does it depend on the operating system?
Does it use PHP's own seeder?
Is it possible to use mt_rand() as generator?
shuffle() function is based on the same generator as rand(), which is the system generator based on linear congruential algorithm. This is a fast generator, but with more or less randomness. Since PHP 4.2.0, the random generator is seeded automatically, but you can use srand() function to seed it if you want.
mtrand() is based on Mersenne Twister algorithm, which is one of the best pseudo-random algorithms available. To shuffle an array using that generator, you'd need to write you own shuffle function. You can look for example at Fisher-Yates algorithm. Writing you own shuffle function will yield to better randomness, but will be slower than the builtin shuffle function.
Update for PHP 7.1
Since the rng_fixes rfc was implemented for PHP 7.1, the implementation of shuffle now utilizes the Mersenne Twister PRNG (i.e. it uses mt_rand and is affected by calling mt_srand).
The legacy system PRNG (rand) is no longer available; the functions rand and srand are in fact aliased to their mt_ equivalents.
Based on Mirouf's answer (thank you so much for your contribution)... I refined it a little bit to take out redundant array counting. I also named the variables a little differently for my own understanding.
If you want to use this exactly like shuffle(), you could modify the parameter to be passed by reference, i.e. &$array, then make sure you change the return to simply: "return;" and assign the resulting random array back to $array as such:
$array = $randArr; (Before the return).
function mt_shuffle($array) {
$randArr = [];
$arrLength = count($array);
// while my array is not empty I select a random position
while (count($array)) {
//mt_rand returns a random number between two values
$randPos = mt_rand(0, --$arrLength);
$randArr[] = $array[$randPos];
/* If number of remaining elements in the array is the same as the
* random position, take out the item in that position,
* else use the negative offset.
* This will prevent array_splice removing the last item.
*/
array_splice($array, $randPos, ($randPos == $arrLength ? 1 : $randPos - $arrLength));
}
return $randArr;
}
It's random just like rand();
And as PHP style you don't need to seed
mt_rand()
Generates a random number.
shuffle()
Randomizes an array. It also generates new keys in the array rather than just rearranging the old ones.
If you want to seed in PHP you would have used mt_strand().
However, since PHP 4.2.0 seeding is done automatically in PHP when you call mt_rand.
Works with associative and numeric arrays:
function mt_shuffle_array($array) {
$shuffled_array = [];
$arr_length = count($array);
if($arr_length < 2) {
return $array;
}
while($arr_length) {
--$arr_length;
$rand_key = array_keys($array)[mt_rand(0, $arr_length)];
$shuffled_array[$rand_key] = $array[$rand_key];
unset($array[$rand_key]);
}
return $shuffled_array;
}
$array = [-2, -1, 'a' => '1', 'b' => '2', 'c' => '3', 11, 'd' => '4', 22];
$shuffled_array = mt_shuffle_array($array);
I've created a function who sort my array randomly.
/**
* Build a random array
*
* #param mixed $array
*
* #return array
*/
function random_array($array) {
$random_array = array();
// array start by index 0
$countArray = count($array) - 1;
// while my array is not empty I build a random value
while (count($array) != 0) {
//mt_rand return a random number between two value
$randomValue = mt_rand(0, $countArray);
$random_array[] = $array[$randomValue];
// If my count of my tab is 4 and mt_rand give me the last element,
// array_splice will not unset the last item
if(($randomValue + 1) == count($array)) {
array_splice($array, $randomValue, ($randomValue - $countArray + 1));
} else {
array_splice($array, $randomValue, ($randomValue - $countArray));
}
$countArray--;
}
return $random_array;
}
It's not the best way to do that but when I've used the function shuffle, it was always returning a random array in the same order. If this could help someone, I will be happy !
I am wondering how I can drop any array items that come after a certain number like 6. Is there something in PHP that enables you do do it? Or is it a custom function that needs to be written
You could use array_slice for this purpose. For example:
$testArray = range(0, 10);
// Ensure there are at least six items in the source array.
if(count($testArray) >= 6) {
// Grab the first six items.
$firstSixItemsFromArray = array_slice($testArray, 0, 6);
}
If you're looking to take the first six elements of an array, based on position in the array, then array_slice or array_splice is the way to go.
array_splice($array, 6);
If you want to keep all elements with value less than 6, you could do something like:
$array = array_filter($array, function($v) { return $v <= 6; });