Random array special - php

how make special random array. i have array(1=>35,2=>25,3=>40). how make possibility that array show special elements. Ex.
If i want get 100 elements from array. it will be
35/100 +-10 - must bee 1 element,
25/100 +-10 = must be 2 element,
40/100 +-10 - must be 3 element.
Elements must be random, but most elements +-10. i know its possible make by this code:
$a = array_fill(1, 35, 1);
$b = array_fill(36, 60, 2);
$c = array_fill(61, 100, 3);
array looks like array(35 elements with value 1, 25 elements with value 2, 40 elements with value 3)
and using merge and array_rand i will get my code. But i don't want this code. it will be create 100 items. need optimization this code. it's possible. help. :-)

Are you looking for a weighted random array?
Check this example:
http://20bits.com/articles/random-weighted-elements-in-php/

Related

Explode Array and Show 4 random items at a time with Shuffle

I am trying to explode an array and then shuffle 4 items at a time so that each array appears in random order.
It half works but on refresh sometimes it shows only 2 or 3 arrays not 4. Is there an easy workaround for it?
Thanks in advance.
$siteswith = $row['siteswith'];
$array = explode(',', $siteswith);
shuffle($array);
foreach(array_slice($array, 0, 4) as $item ){
}
It seems that $array does not have enough items that the slice function can "slice" from. Please check if your input string $row['siteswith'] always provide 4 or more comma separated values.
Depending what the code is later used for, you could also use array_rand() to pick n random elements from the list
$siteswith = $row['siteswith'];
$array = explode(',', $siteswith);
foreach(array_rand($array, 4) as $item ){
...
}
This will also prevent mutating your origin array (other than array_shift will do) which avoids unexpected behaviour.
Please be aware that you'll receive an exception when the array size is smaller than the requested amount of random elements.

How to get random item from array only from the specified keys (e.g x key first)

I have a array that i want to select two item as random, And two keys are selected between the key 2 and the key 8.
$arr = array=(1,2,3,4,5,6,7,8,9,10,11,12);
I can get two random item:
$rand_keys = array_rand($arr, 2);
$arr[$rand_keys[0]]; // one
$arr[$rand_keys[0]]; // two
In the above code, It is possible that selected items be from the entire array. Now i want to know how can I limit my choice?
In fact i want to get random item from this array:
array=(2,3,4,5,6,7,8);
You can use array_slice to first take a part of the array before you take random items from it
$part = array_slice($arr, 1, 7); // outputs array(2,3,4,5,6,7,8)

Using array_slice() to get first few values

I'd like to use an array_slice to get every value in an array iterated over, except for the current one, the one before it, and all the others until the end of the array.
So for example, say I have 5 elements:
[1, 2, 3, 4, 5]
I want a way for a condition to fire on 3, cut it out, and then also cut out 4 and 5. I have this, but I don't think it's correct:
$items = array_slice($items, $itemcount - 1);
array_slice() returns the sequence of elements from the array array as specified by the offset and length parameters.
So if you want to get the 3,4,5 your offset should be 2, because the array key ( starts from 0 ex: 0,1,2,3,4). i didn't use length in this one because i want to get it till the end (5)
$items = array_slice($items, 2);
If you want to grag 1,2 it should be like this. ( i use length 2 because i want to get the first 2 keys)
$items = array_slice($items, 0, 2);
Example

PHP: Random list of 52, so when repeated it's unique from the previous list?

I'm writing a game in PHP. There are 52 elements in an array 0 to 51. The goal is to create a random list each time that is unique from the previous list. So that there is no sequence pattern from the first list in the next one. For example, if the first list has a pattern contained as 7654, we don't want a pattern of 7654 to appear immediately in the next list.
The rule is that the first element of the list and the last element can't be the start and end elements from the previous list. For example if the first list starts and ends with:
0
...
51
Don't want the next list to start with 51 and end with 0.
I'm been thinking about how to approach this. I'm aware of the shuffle function in PHP, but each time it generates a random list it has no knowledge of the previous list. Again, the lists need to be unique directly from it's previous list, not from any other list it generates for the player.
The overall goal is that each list has no relationship or common pattern to it's previous list.
I don't know if there is a name for these kind of random lists.
How could this be done in PHP? Thanks!
Might this be done by using a random method which uses a different "seed" number each time it randomizes the list?
The pattern sequence is 4. So if 7654 appeared in the previous list, then the new list can't have 7654 in it.
I think I've got a solution that should work to meet these constraints.
function nextList($last) {
$index = rand(1,50); // don't select first or last elements
$out = array();
do {
list($value) = array_splice($last, $index, 1);
$out[] = $value;
$maxLoop = count($last);
do {
$newIndex = array_rand($last);
} while ($newIndex == $index && --$maxLoop);
$index = $newIndex;
} while (count($last) > 1);
$out[] = $last[0];
return $out;
}
Used as follows:
$first = range(0, 51);
shuffle($first);
$second = nextList($first);
$third = nextList($second);
// etc.
This works on the basis that, for each element it adds to the new array, it is not proceeded by the same element that proceeds it in the last array.
For example, if the last array had, somewhere, the values 43,12,13..., and we add the element 43 to the new array, then we ensure that the next element is any element EXCEPT 12. With this logic, it should be impossible to get the same repeated sequences.
It also ensures that the first element is neither the first nor last element of the last array, however, making this constraint work also for the last element of the array would be a lot more difficult.
EDIT
I was actually able to use the check function given in Hendriq's answer to check whether this solution works, and it seems to always return a valid new list which does not contain a sequence from the last array =]
Well there are is a small problem with the assumptions you have
Rule 1
The overall goal is that each list has no relationship or common pattern to it's previous list.
Rule 2
The rule is that the first element of the list and the last element can't be the start and end elements from the previous list.
Those two contradict each other. First there may be no relationship, so random. So as you said shuffle (did not look at is I take your word). But the second one needs to know about the previous one. What contradicts with your previous rule.
But what you could do is have the first and last element of the previous draw (the previous 52). Then you shuffle the current draw. If the first and/or last element are the same, draw again until no match is found.
Also I think the word for this is semi-random.
Look at the do{ }while(); for the trick.
After the comment of not having the same 4 elements after each other the next part came.
What you need is way to identify the elements, so lets assume every "item" has an id (for the purpose of the example I only use 5 elements)
Lets use the following
array(
0 => array('id' => 1,),
1 => array('id' => 2,),
2 => array('id' => 3,),
3 => array('id' => 4,),
4 => array('id' => 5,),
)
The next draw is exaclty the same (just for the purpose of the exaple). What we do is we make an array of the ids that are drawn:
array(
0 => 1,
1 => 2,
2 => 3,
3 => 4,
4 => 5,
)
Then use this array in the following function (not checked but should give you a right start)
function check(array $prev, array $draw, $the_same = 4) {
$to_check = count($prev) - $the_same;
for($i = 0; $i < $to_check; $i++) {
if ($array_slice($prev, $i, $the_same) === array_slice($draw, $i, $the_same)) {
return false;
}
}
return true;
}
What is does, you give it the old array and the one you drew, and you give the number with it with how many occurence may be after eachother that maybe the same. Then it just start looping and array slicing to check if they are equal.

PHP keep first 6 items in an array and drop the rest

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; });

Categories