I have created a small script which gives me random numbers. Now I want to create a list of this random number list with repeating numbers.
It should generate match numbers of this list of random numbers (A match number is a number which repeats)
For instance 4 – 6 – 9 – 32 – 34 – 31 – 5 – 32 (The match number here is 8 because after 8 numbers we have a repeater). I would like all the match numbers to be echo beside each other with a space in between.
Can someone help me?
I have tried creating an if statement but I can't get it working.
for ($rnd=1;$rnd<=50;$rnd++)
{
$random = mt_rand(0,100) . " ";
echo $random;
}
Explanation coming soon.
$numbers = array();
for($i = 1; $i <= 50; $i++) {
$number = mt_rand(0,100);
if(!isset($numbers[$number])) $numbers[$number] = array();
$numbers[$number][] = $i;
}
foreach($numbers as $key => $value) {
$start = '';
foreach($value as $k => $v) {
echo $start . $key . ' (Match Number: ' . $v . ')';
$start = ' - ';
}
echo '<br />';
}
If I don't misunderstood your question then this is what you want. Let's do this way-
<?php
$existing = [];
$repeat_numbers = [];
for ($rnd=1;$rnd<=50;$rnd++)
{
$random = mt_rand(0,100);
if(in_array($random,$existing)){
$repeat_numbers[] = $rnd; // pushing the repeating index
}
$existing[] = $random;
echo $random.PHP_EOL;
}
echo implode('-',$repeat_numbers);
?>
WORKING DEMO: https://3v4l.org/eEhB8
AS PER THE COMMENT
<?php
$existing = [];
$repeat_numbers = [];
for ($rnd=1;$rnd<=50;$rnd++)
{
$randoms[] = mt_rand(0,100);
}
echo implode('-',$randoms).PHP_EOL;
$i = 1;
foreach($randoms as $rnd){
if(in_array($rnd,$existing)){
$repeat_numbers[] = $i;
$i=1;
}
$existing[] = $rnd;
$i++;
}
echo implode('-',$repeat_numbers);
?>
WORKING DEMO https://3v4l.org/Xjc5X
AS PER THE LATEST COMMENT
<?php
$existing = [];
$repeat_numbers = [];
$randoms = explode('-','3-31-34-29-28-5-28-23-31-4-1-31-11-17-23-9-20-24-22-3-11-24-26-4-10');
$i = 1;
foreach($randoms as $rnd){
if(in_array($rnd,$existing)){
$repeat_numbers[] = $i;
$i=1;
$existing = []; // This like will do the magic for you
}
$existing[] = $rnd;
$i++;
}
echo implode('-',$repeat_numbers);
?>
WORKING DEMO: https://3v4l.org/hIT22
FINAL EDIT:
<?php
$existing = [];
$repeat_numbers = [];
$randoms = explode('-','4-9-13-18-19-34-23-9-9-13-44-5-13-13-88-26-29-27-34-67-65-83-26');
$i = 1;
foreach($randoms as $rnd){
if(in_array($rnd,$existing)){
$repeat_numbers[] = $i;
$i=1;
$existing = [];
}else{
$existing[] = $rnd;
$i++;
}
}
echo implode('-',$repeat_numbers);
?>
WORKING DEMO: https://3v4l.org/9j7iq
EDITED AGAIN:
<?php
$existing = [];
$repeat_numbers = [];
$randoms = explode('-','4-9-13-18-19-34-23-9-9-13-44-5-13-13-88-26-29-27-34-67-65-83-26');
//print_r($randoms);
$i = 0;
foreach($randoms as $rnd){
$i++;
if(in_array($rnd,$existing)){
$repeat_numbers[] = $i;
if(count($existing)==1){
$i=1;
}else{
$i=0;
}
$existing = [];
}
$existing[] = $rnd;
//print_r($existing);
}
echo implode('-',$repeat_numbers);
?>
WORKING DEMO: https://3v4l.org/VfIJY
Related
When the number 6 is giving I need to calculate till 6: for example: 1+2+3+4+5+6=21. I also need to add the sums of each to an array like: 1+2=3, 1+2+3=6, 1+2+3+4=10, ...
I have tried to make the while loop to be printed and this working fine but nog for array purpose:
<?php
$number = $_POST['number'];
$i = 1;
$cal = 0;
$tussenBerekening = array();
while ($i <= $number) {
echo $i;
$cal = $cal + $i;
array_push($tussenBerekening, $cal);
if ($i != $number) {
echo " + ";
} else {
echo " = " . $cal;
}
$i++;
}
?>
This is my new code, it prints, but no total sum.
<?php
$number = $_POST['number'];
$i = 2;
$cal = 0;
$sum = 1;
$berekeningen = array();
while ($i <= $number) {
$sum .= "+" . $i;
array_push($berekeningen, $sum);
$i++;
}
print_r($berekeningen);
?>
Here's a solution:
$i = 1;
$number = 6;
while ($i <= $number) {
// generate array with values from 1 to $i
$array = range(1, $i);
// if there're more than 1 element in array - output sum
if (count($array) > 1) {
// 1+2+... part // sum of elements of array
echo implode('+', $array) . ' = ' . array_sum($array) . '<br />';
}
$i++;
}
I have problem assigning values to the $word as array so that I can make it as a variable.
I am displaying an output of
1,000,000,000 they have all different echos; I want to make them as 1 value only example:
$output = $arramt[1]="1",$arramt[2]="000",$arramt[3]="000",$arramt[4]="000"
function mnyFmt($word){
$n = strlen($word);
if ($n%3==0){
$i = $n/3;
$x=0;
while($x<$i-1){
echo substr($word,-$n,3).",";
$arramt[$x] = substr($word,-$n,3).",";
$x++;
$n = $n-3;
}
echo substr($word,-$n,3);
$arramt[$x] = substr($word,-$n,3).",";
}
elseif($n%3==1){
$word1 = substr($word,1,$n);
$w1 = strlen($word1);
$i = $w1/3;
echo substr($word,0,1).",";
$x=0;
while($x<$i-1){
echo substr($word1,-$w1,3).",";
$arramt[$x] = substr($word1,-$w1,3).",";
$x++;
$w1 = $w1-3;
}
echo substr($word1,-$w1,3);
$arramt[$x] = substr($word1,-$w1,3).",";
echo array_values($arramt[$x]);
}
elseif($n%3==2){
$word1 = substr($word,2,$n);
$w1 = strlen($word1);
$i = $w1/3;
echo substr($word,0,2).",";
$x=0;
while($x<$i-1){
echo substr($word1,-$w1,3).",";
$x++;
$w1 = $w1-3;
}
echo substr($word1,-$w1,3);
}
}
$amount = 1000000000;
mnyFmt($amount);
}
Thanks very much in advance.
You don't need to make any of the custom function you can simply use number_format and explode function like as
$amount = 1000000000;
print_r(explode(',',number_format($amount)));
Demo
I have a 6x6 bingo card which is generated with random numbers between 10 and 70. The 7th row and 7th column are used to count the drawn numbers that are on the card. When a row or column reaches 6, there is bingo.
The eventual result I get is right, but in the proces of getting there a few things are going wrong.
In my function generateCard I create the rows and columns of numbers for the card. I think the problem lies in this function.
function generateCard()
{
$card = array();
for ($row = 1; $row < 8; ++$row)
{
$card[$row] = array();
$deck = array(0,1,2,3,4,5,6,7,8,9);
for ($kolom = 1; $kolom < 8; ++$kolom) {
$index = mt_rand(0,count($deck) - 1);
$number = $deck[$index];
$card[$row][] = $row . $number;
unset($deck[$index]);
$deck = array_values($deck);
}
//Test
printCard($card);
// 7th column
$card[$row][7] = 0;
}
//Test
printCard($card);
// 7th row
for ($kolom = 1; $kolom < 7; ++$kolom){
$card[7][$kolom] = 0;
}
//Test
printCard($card);
return $card;
}
I've put in a few printCard functions to test the outcome.
The first test above the creation of the 7th column gives me a ton of undefined offset 7 notices. I figured this happens because the column does not exist, but when I try to create this earlier, I still get the notices.
The second test above the 7th row shows that the 7th row (which should be 0) gets filled with numbers. This gets overwritten after with 0 values. I figured I could fix this by putting $row < 7 and $kolom < 7, but when I do this the card won't get printed right at all.
I'm wondering why i'm getting all these undefined offset notices about the last column (even when I create it earlier) and why I can't use $row < 7 and $column < 7 in both the for loops of generate card to avoid filling up the last row and column (7x7) with values. These should be 0 before the bingo game starts.
Do you have any suggestions?
I think i'm overlooking a few things here..
I will put the complete code here:
mt_srand((double)microtime()*1000000);
function generateCard()
{
$card = array();
for ($row = 1; $row < 8; ++$row)
{
$card[$row] = array();
$deck = array(0,1,2,3,4,5,6,7,8,9);
for ($kolom = 1; $kolom < 8; ++$kolom) {
$index = mt_rand(0,count($deck) - 1);
$number = $deck[$index];
$card[$row][] = $row . $number;
unset($deck[$index]);
$deck = array_values($deck);
}
//Test
printCard($card);
// 7th column
$card[$row][7] = 0;
}
//Test
printCard($card);
// 7th row
for ($kolom = 1; $kolom < 7; ++$kolom){
$card[7][$kolom] = 0;
}
//Test
printCard($card);
return $card;
}
function printCard($card){ ?>
<table border="1" cellspacing="0" cellpadding="5">
<?php for($rij = 1; $rij < 8; $rij++) { ?>
<tr>
<?php for ($kolom = 1; $kolom < 8; $kolom++) { ?>
<td<?php if (($card[$rij][7] == 6) || ($card[7][$kolom] == 6)) { echo ' style="background-color:green"'; } ?>><?php echo $card[$rij][$kolom]; ?></td>
<?php }
} ?>
</tr>
</table>
<?php }
$card = generateCard();
$getrokkenGetallen = array();
$deck = range(10,69);
$bingo = false;
while (!$bingo){
$index = mt_rand(0,count($deck) - 1);
$number = $deck[$index];
if(!in_array($number, $getrokkenGetallen)){
unset($deck[$index]);
$deck = array_values($deck);
$getrokkenGetallen[] = $number;
for ($row = 1; $row < 7; $row++) {
for ($kolom = 1; $kolom < 7; $kolom++) {
if ($card[$row][$kolom] == $number) {
$card[$row][7] += 1;
$card[7][$kolom] += 1;
if(($card[$row][7] == 6) || ($card[7][$kolom] == 6)){
$bingo = true;
}
break;
}
}
}
}
}
echo '<h2>Bingokaart waarop BINGO is gevallen</h2>';
printCard($card);
echo '<p><strong>Getrokken getallen:</strong><br>';
foreach($getrokkenGetallen as $value)
{
echo $value . ' ';
}
echo '</p>';
echo '<p><strong>Aantal getallen dat is getrokken:</strong> ';
echo count($getrokkenGetallen);
echo '</p>';
Example of the output:
Thank you in advance for any help or suggestions.
I'm not exactly sure where your error comes from, and I'm not a regular PHP user, but if i change the generateCard function to this:
function generateCard(){
$card = array();
for ($row = 1; $row < 8; ++$row){
$card[$row] = array();
$deck = range($row*10, $row*10+9);
shuffle($deck);
for($col = 1; $col < 8; ++$col){
if($row == 7 OR $col == 7){
$card[$row][$col] = 0;
}else{
$card[$row][$col] = $deck[$col];
}
}
}
//Test
printCard($card);
return $card;
}
It all works as expected. It's a little simpler than the route you were going i think.
I am creating one algorithm for kids that will create random numbers and showing 5 numbers in each row upto 6 rows.for whole numbers it is good.But when i am taking random numbers between a negative and a positive number then there is possibility of negative result.So what should i write so that the result will be only positive number or zero irrespective of taking negative numbers? Below is my code,
$Final_Array = array();
$Final_Ansarray = array();
for ($i = 1; $i < 31; $i++) {
$Random_Number = mt_rand(-4,8);
$RandVal = $Random_Number . "";
array_push($Final_Array, $RandVal);
}
$Chunked_Array = array_chunk($Final_Array, 5);
foreach ($Chunked_Array as $key => $value) {
foreach ($value as $key2 => $value2) {
echo $value2 . "\n";
}
echo "|";
}
foreach ($Chunked_Array as $k => $subArray) {
$Answers = array_sum($subArray);
echo $Answers . "|";
array_push($Final_Ansarray, $Answers);
}
Try this
$cols = 5;
$rows = 6;
$array = array();
for ($j = 0; $j < $rows; $j++)
{
do
{
for ($i = 0; $i < $cols; $i++)
$array[$i] = mt_rand(-4, 8);
}
while (array_sum($array) < 0); // pass only non negative sums
echo'sum', json_encode($array), ' = ', array_sum($array), PHP_EOL;
}
Example output
sum[1,-1,2,8,-2] = 8
sum[1,5,5,5,-4] = 12
sum[2,7,-1,6,0] = 14
sum[8,0,-2,-1,0] = 5
sum[7,-2,8,4,2] = 19
sum[7,-4,-4,0,1] = 0
I want to show on first page randomly selected links from a dati.txt file (from about 1000 links 5 randomly selected) from this:
<?php
$righe_msg = file("dati.txt");
$numero=0;
foreach($righe_msg as $riga)
{
$dato = explode("|", trim($riga));
if (trim($dato[5])=="yes")
{
$numero++;
echo"<tr><td bgcolor='#EEEEEE'>» <a href='$dato[2]'> $dato[1]</a></td></tr> ";
}
}
?>
About dati.txt
dati.txt is made like this
date1.. |title1..|link1...|description1|email1|yes
date2.. |title2..|link2...|description2|email2|yes
date3.. |title3..|link3...|description3|email3|yes
date4.. |title4..|link4...|description4|email4|yes
date5.. |title5..|link5...|description5|email5|yes
date6.. |title6..|link6...|description6|email6|yes
..
But how do you get for example (links) out with this code:
$links = file("dati.txt");
$numLinks = count($links);
$tmp = array();
for ($i = 0; $i < min(5, $numLinks); $i++)
{
$randomIndex = rand(0, $numLinks - 1);
$randomLink = $links[$randomIndex];
// Avoid duplicates:
if (in_array($randomLink, $tmp))
{
$i--;
continue;
}
$tmp[] = $randomLink;
echo $randomLink;
}
Thanks
<?php
$links = file('links.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
// used this for testing (generated from a file with one link each line (+carriage return)
//$links = ARRAY('foo 1','foo 2','foo 3','foo 4','foo 5','foo 6','foo 7','foo 8','foo 9','foo 10','foo 11','foo 12');
$amount = 3;
shuffle($links);
$rand_list = array_slice($links, 0, $amount);
foreach ($rand_list AS $key => $link) {
print $link.'</br>';
}
?>
Considering 1 link per line:
$links = file("dati.txt");
$numLinks = count($links);
$randomIndex = rand(0, $numLinks - 1);
$randomLink = $links[$randomIndex];
echo $randomLink;
Getting more links is simply a matter of a loop:
$links = file("dati.txt");
$numLinks = count($links);
$tmp = array();
for ($i = 0; $i < min(5, $numLinks); $i++)
{
$randomIndex = rand(0, $numLinks - 1);
$randomLink = $links[$randomIndex];
// Avoid duplicates:
if (in_array($randomLink, $tmp))
{
$i--;
continue;
}
$tmp[] = $randomLink;
echo $randomLink;
}
$righe_msg = file("dati.txt");
$num = 5;
$selected = array();
while (count($selected)!=$num)
{
$rand = rand(0,count($righe_msg)-1);
$selected[$rand] = $righe_msg[$rand];
}
$numero=0;
foreach ($selected as $sel)
{
$dato = explode("|", trim($sel));
if (trim($dato[5])=="yes")
{
$numero++;
echo"<tr><td bgcolor='#EEEEEE'>» <a href='$dato[2]'> $dato[1]</a></td></tr> ";
}
}