How to create a looping random number generator - php

I need to create a looping random number generator so each loops pumps out a different set of random numbers.
e.g result would be:
9463216549
6541335466
6749746326
6546879994
Code I have so far is:
<?php
$limit1 = 10;
$counter1 = 1;
$limit2 = 4;
$counter2 = 1;
while ($counter2 <= $limit2) {
while ($counter1 <= $limit1) {
$rayndom = mt_rand(0,6);
$counter1++;
}
$counter2++;
}
?>

If you are using php version > 7 you can use inbuilt function random_int():
Generates cryptographically secure pseudo-random integers
Usage of random_int():
random_int(0, 1000); // 0 is min value and 1000 is max
random_int() is always safe alternative to rand() and mt_rand()
If you are using PHP version < 7.0 then you can take a look at userland implementation of random_int i.e. random_compat.

Here is the code hope this helps
<?php
$waves = array(
array(),
array(),
array(),
array()
);
foreach($waves as $wave) {
for($counter = 1; $counter <= 10; $counter++) {
$num = mt_rand(0,6);
array_push($wave, $num);
}
print_r($wave);
}
?>
Your array will look like this
Array
(
[0] => 0
[1] => 2
[2] => 1
[3] => 0
[4] => 2
[5] => 1
[6] => 2
[7] => 6
[8] => 5
[9] => 0
)
Array
(
[0] => 4
[1] => 2
[2] => 2
[3] => 5
[4] => 5
[5] => 5
[6] => 6
[7] => 0
[8] => 4
[9] => 2
)
Array
(
[0] => 5
[1] => 4
[2] => 2
[3] => 5
[4] => 4
[5] => 3
[6] => 0
[7] => 3
[8] => 5
[9] => 2
)
Array
(
[0] => 2
[1] => 1
[2] => 4
[3] => 5
[4] => 0
[5] => 2
[6] => 4
[7] => 4
[8] => 4
[9] => 6
)
each array will be one wave

Related

Swiss Tournament. Team confrontation multiple rounds without repeat and ordered

I try to make a tournament based on Swiss method.
I have for example 12 teams, and in each round, every team has points (0 to 100) and wins, loses or draws.
I want to find which teams play against each other, with these conditions:
Ordered by wins, draws, and points.
Not to be played previously.
In each round, I got foreach team the possible teams to play against in an array like this: (The key indicates the team id, and the values indicate ll possible teams to play separated by ",")
[2] => 4,11,6,10,3,8,7,12,
[5] => 4,11,9,10,3,8,1,12,
[4] => 2,5,6,10,8,7,12,
[11] => 5,9,10,3,8,7,
[9] => 5,11,6,3,8,7,12,
[6] => 2,4,9,10,3,7,12,
[10] => 2,5,4,11,6,8,7,12,
[3] => 5,11,9,6,8,7,
[8] => 2,5,4,11,9,10,3,1,12,
[7] => 2,4,11,9,6,10,3,1,12,
[1] => 5,4,11,9,6,3,8,7,
[12] => 2,5,4,9,6,10,8,7,
First I have all teams played before in a array: (key indicates team id)
Array (
[1] => 2,10,12,
[2] => 1,9,5,
[3] => 4,12,10,
[4] => 3,11,9,
[5] => 6,7,2,
[6] => 5,8,11,
[7] => 8,5,8,
[8] => 7,6,7,
[9] => 10,2,4,
[10] => 9,1,3,
[11] => 12,4,6,
[12] => 11,3,1, )
Then, I get all teams ordered by wins, loses and points in a array: (Keys indicates also team id)
Array
(
[1] => 2
[2] => 5
[3] => 4
[4] => 11
[5] => 9
[6] => 6
[7] => 10
[8] => 3
[9] => 8
[10] => 7
[11] => 1
[12] => 12
)
Finaly I try to find the possible match against two teams.
$checks = array();
$pairs = array();
for ($i = 1; $i <= count($list); $i++) {
for ($j = 1; $j <= count($list); $j++) {
if(strpos($plays[$list[$i]], $list[$j]) !== false || $list[$i] == $list[$j] ) {
}else{
if(!in_array($list[$i],$checks) && !in_array($list[$j],$checks)){
$pairs[] = $list[$i].",".$list[$j];
$checks[] = $list[$i];
$checks[] = $list[$j];
}
}
}
}
And finaly I print the array "$pairs". It shows:
Array
(
[0] => 2,4
[1] => 5,11
[2] => 9,6
[3] => 10,8
[4] => 3,7
)
Thats not correct because the team_id 1 and team_id 12 can not play in this round because the played before:
I don't know how to solve this.
Thanks again!

how to search array by specifying the index to start search from in php

how to search array by specifying the index to start search from in php
for example:
Assuming
$needle_to_search = 5;
$array_to_search = [6,8,4,9,7,5,9,4,5,9,3,5,4,6,7];
$index_to_start_search_from = 5;
$key = array_search($needle_to_search, $array_to_search, $index_to_start_search_from);
is there any function that can implement above pseudo code
so that $key will return index: 8
what i mean is that i want to start the search from index 6 so that it returns index 8=> which has the value 5, since value 5 is my aim to search in the array.
i think you can do with array_slice.
$needle_to_search = 5;
$array_to_search = [6,8,4,9,7,5,9,4,5,9,3,5,4,6,7];
$index_to_start_search_from = 6;
// output
Array
(
[0] => 6
[1] => 8
[2] => 4
[3] => 9
[4] => 7
[5] => 5
[6] => 9
[7] => 4
[8] => 5
[9] => 9
[10] => 3
[11] => 5
[12] => 4
[13] => 6
[14] => 7
)
// return 5
echo array_search($needle_to_search, $array_to_search);
// return 8
echo array_search($needle_to_search, array_slice($array_to_search, $index_to_start_search_from, null, true));

How to combine two arrays like this?

$blueswards array:
Array ( [0] => 3 [1] => 8 [2] => 1 [3] => 4 [4] => 9 )
$redswards array:
Array ( [0] => 2 [1] => 9 [2] => 3 [3] => 6 [4] => 9 )
What i try to do:
Array ( [0] => 3 [1] => 8 [2] => 1 [3] => 4 [4] => 9 [5] => 2 [6] => 9 [7] => 3 [8] => 6 [9] => 9 )
I cant do it with array_merge.
EDIT = Sorry everyone i did it with array merge now its working. :[
I used array_merge() using your array values. And, it worked.
<?php
$new_array[] = array();
$blueswards = array(3,8,4,9);
$redswards = array(2,9,3,6,9);
$new_array = array_merge($blueswards,$redswards);
print_r($new_array);
?>
OUTPUT:
Array ( [0] => 3 [1] => 8 [2] => 4 [3] => 9 [4] => 2 [5] => 9 [6] => 3 [7] => 6 [8] => 9 )
Why don't you just run them in a loop once they're populated.
$newarray=0;
$arrayCounter=0;
for (i=0; i<count($redswards)-1 ; i++){
$newarray[$arrayCounter] = $redswards[i];
$arrayCounter++
}
for (i=0; i<count($blueswards)-1 ; i++){
$newarray[$arrayCounter] = $blueswards[i];
$arrayCounter++
}
There... That will do it correctly by simply adding on to the new array. It will also count the number of times it has to loop so you don't have to hardcode that.
I mean, that's really simple, and you may have to change the hardcoded i values. But that's the basic idea.
Used durbnpolsns code and edited it to work.
$newarray=0;
for ($i=0; $i<4 ; $i++){
$newarray[$i] = $redswards[$i];
}
$j=0;
for ($i=5; $i<8 ; $i++){
$newarray[$i] = $blueswards[$j];
$j++;
}
I have not tested the code and formatting may be impossible, but I'm typing on my phone.
Sorry for that.

Reading a file line by line and extract values

I need to do a routine that is able to extract values from a file and send these values to a mySQL database.
However my routine is returning some, imo, odd results.
This is my code:
$file = new SplFileObject($file_name);
for($i = 1 ; $i <= 5 ; $i++)
//while (!$file->eof())
{
$linha = $file->fgets();
echo $linha.'<br>';
$line = explode(" ", $linha);
print_r($line);
echo '<br'>;
}
$file = null;
I used to have a while loop to run the whole file, but was getting an error and then decided to do a for loop to have a smaller number of results.
The results I get are:
1 1412114400 100 20 10 2 1 80 15 8 1.5 true 20 5
Array ( [0] => 1 [1] => 1412114400 [2] => 100 [3] => 20 [4] => 10 [5] => 2 [6] => 1 [7] => 80 [8] => 15 [9] => 8 [10] => 1.5 [11] => true [12] => 20 [13] => 5 )
Array ( [0] => )
2 1412114460 100 20 10 2 1 80 15 8 1.5 true 20 5
Array ( [0] => 2 [1] => 1412114460 [2] => 100 [3] => 20 [4] => 10 [5] => 2 [6] => 1 [7] => 80 [8] => 15 [9] => 8 [10] => 1.5 [11] => true [12] => 20 [13] => 5 )
Array ( [0] => )
3 1412114520 100 20 10 2 1 80 15 8 1.5 true 20 5
Array ( [0] => 3 [1] => 1412114520 [2] => 100 [3] => 20 [4] => 10 [5] => 2 [6] => 1 [7] => 80 [8] => 15 [9] => 8 [10] => 1.5 [11] => true [12] => 20 [13] => 5 )
It seems that for every other cicle the fgets() function returns a "\n\r" and I don't know how to get rid of it.
EDIT:
I've used trim() and str_replace() and got the same result, so maybe I'm not getting a "\n\r".
<?php
$file = new SplFileObject($file_name);
$i = 1;
while ($i <= 5) {
$linha = trim($file->fgets());
if ($linha == '') {
continue;
}
echo $linha.'<br>';
$line = explode(" ", $linha);
print_r($line);
echo '<br>';
$i++;
}
$file = null;
to get rid of \r\n use:
$line = str_replace(array("\r", "\n", "\r\n"), array('', '', ''), $line);
I would also recommend using file_get_contents() as it offers a much cleaner way of reading files: http://php.net/manual/en/function.file-get-contents.php
You are also missing a > off your second <br>

2 arrays count how many times id = date

Ive sat with this for a while now, and its getting late.
Im trying to get a top 3 of most sales from last month, and i need to count how many times a id from array 1 is equal to array 2 last month(6 = last atm.) like id 4 = 2, id 7 = 3
It might not be the perfect solution, but im just trying to break it down by my self, so later on 'maybe' problems, will i take care of when i hit the wall,
so please, if anyone can help me here, ill be greatfull.
UPDATE
- I will add the result im looking for here: (sorry i didnt before, it makes it alot easier :-)
- The result below, is because i want the count from 2014-06-01 and up to the last day of that month monly, on array[0][1] under this array, only 6-7-8 is not from 2014-06
Hope it makes a bit more sense now ^^
Array
(
[0] => Array
(
[0] => Array
(
[4] => 2
[7] => 3
[1] => 2
[3] => 2
[9] => 1
[12] => 1
[2] => 1
[13] => 1
)
)
)
Array
(
[0] => Array
(
[0] => Array
(
[0] => 4
[1] => 4
[2] => 7
[3] => 1
[4] => 7
[5] => 7
[6] => 3
[7] => 3
[8] => 4
[9] => 9
[10] => 12
[11] => 2
[12] => 13
[13] => 1
)
[1] => Array
(
[0] => 2014-06-18
[1] => 2014-06-10
[2] => 2014-06-05
[3] => 2014-06-05
[4] => 2014-06-12
[5] => 2014-06-11
[6] => 2013-12-12
[7] => 2014-07-23
[8] => 2014-05-13
[9] => 2014-06-01
[10] => 2014-06-12
[11] => 2014-06-04
[12] => 2014-06-04
[13] => 2014-06-11
)
)
)
I hope that what I understood is what you're really asking for. let's say your array definition is :
$arr = Array
(
0 => Array
(
0 => Array
(
0 => 4,
1 => 4,
2 => 7,
3 => 1,
4 => 7,
5 => 7,
6 => 3,
7 => 3,
8 => 4,
9 => 9,
10 => 12,
11 => 2,
12 => 13,
13 => 1
),
1 => Array
(
0 => "2014-06-18",
1 => "2014-06-10",
2 => "2014-06-05",
3 => "2014-06-05",
4 => "2014-06-12",
5 => "2014-06-11",
6 => "2013-12-12",
7 => "2014-07-23",
8 => "2014-05-13",
9 => "2014-06-01",
10 => "2014-06-12",
11 => "2014-06-04",
12 => "2014-06-04",
13 => "2014-06-11"
)
)
);
If you need to test if the date is lower than 6 month and then put their id, sales and date you need to use this code
$result = [];
$index=0;
foreach ($arr[0][0] as $key => $value)
{
$date1 = new DateTime($arr[0][1][$key]);
$date2 = new DateTime();
$diff = $date1->diff($date2);
$diff = ($diff->format('%y') * 12) + $diff->format('%m');
if($diff<=6)
{
$result[$index]['id'] = $key;
$result[$index]['sales'] = $value;
$result[$index]['date'] = $arr[0][1][$key];
$index++;
}
}
var_dump($result);
array_count_values() will give you the number of times each value appears in array 1.
$count = array_count_values($array[0][0]);
Result:
Array
(
[4] => 3
[7] => 3
[1] => 2
[3] => 2
[9] => 1
[12] => 1
[2] => 1
[13] => 1
)
Then you can use a loop to combine with array 2:
$result = array();
foreach($count as $key=>$val) {
$result[$array[0][1][$key]] = $val;
}
Result:
Array
(
[2014-06-12] => 3
[2014-07-23] => 3
[2014-06-10] => 2
[2014-06-05] => 1
[2014-06-01] => 1
[2014-06-04] => 1
[2014-06-11] => 1
)
See demo

Categories