Jump to content

Recommended Posts

67          88          109        61          147

134        165        100        118        106

46          50          70          55          37

82          94          103        97          155

39          79          54        112          43

            190        52        138          115

            146        150        85          130

                          81        124

                                      73

                                      127

Link to comment
https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266374
Share on other sites

i can garantee your parser engine wont like you very much... but how bout somin like this?

 

$array1=array("67","134","46","82","39");
$array2=array("88","165","50","79","190","146");
$array3=array("109","100","70","103","54","52","150","81");
$array4=array("61","118","55","97","112","138","85","124","73","127");
$array5=array("147","106","37","155","43","115","130");
$numbers=array();
while($all!=484 && count($numbers)!=5){
if(count($numbers)==0){
  $numbers[]=$array1[array_rand($array1)];
  $all+=$array1[array_rand($array1)];
}elseif(count($numbers)==1){
  $numbers[]=$array2[array_rand($array2)];
  $all+=$array2[array_rand($array2)];
}elseif(count($numbers)==2){
  $numbers[]=$array3[array_rand($array3)];
  $all+=$array3[array_rand($array3)];
}elseif(count($numbers)==4){
  $numbers[]=$array4[array_rand($array4)];
  $all+=$array4[array_rand($array4)];
}elseif(count($numbers)==5){
  $numbers[]=$array5[array_rand($array5)];
  $all+=$array5[array_rand($array5)];
}
if(count($numbers)==5 && $all!=484){
  $numbers=array();
  $all=0;
}
}

not tested...

Link to comment
https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266392
Share on other sites

Its a tough one man. Definitely a brain strain. Because you would first need to start with the first number in the first column, match that up against all possibilities for the other 5 categories and check the sums. Unfortunately I do not think there is anything that will help you here really. I am not sure if recursion would be right or not.

 

How I envision it is you have a master array, which houses the 5 categories, the first category is the main category to do the checks against. You would have to loop through the first category checking each possibility of the other 4 categories against it. I am not sure if recursion would help out, or if you want the foreach or the for statement.

 

I try and do some code but for now it is not clicking if I come up with it I will be sure to post it but yea, this is a pretty intense code you want (at least with the knowledge I have).

 

Taith's example seems like it would work, but wow that could take a long ass time, but yea if I think of a better way I will sure post it.

Link to comment
https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266401
Share on other sites

It only took about 20-30 seconds, it's not bad, I can kick this server's ass it's okay, and taith yes I did try and print the array and no error comes up but thats just my stupid setup, but I know it hit an error cause I put some html writing below and it would have shown that even if the php failed.

Link to comment
https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266406
Share on other sites

Yea, it only took that long for the results you had, but the time will also vary every time you retry the code with the random array part, potentially there is a small line that this could run for 10 minutes given how many results etc.  as it is all randomized.

 

Anyhow it works, I am still going to try and rack my brain over something like that, but more efficient for future use, it will be an interesting challenge.

Link to comment
https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266409
Share on other sites

$array1=array("67","134","46","82","39");
$array2=array("88","165","50","79","190","146");
$array3=array("109","100","70","103","54","52","150","81");
$array4=array("61","118","55","97","112","138","85","124","73","127");
$array5=array("147","106","37","155","43","115","130");
$numbers=array();
while($all!=484 && count($numbers)!=5){
if(count($numbers)==0){
  $numbers[]=$array1[array_rand($array1)];
  $all+=$array1[array_rand($array1)];
}elseif(count($numbers)==1){
  $numbers[]=$array2[array_rand($array2)];
  $all+=$array2[array_rand($array2)];
}elseif(count($numbers)==2){
  $numbers[]=$array3[array_rand($array3)];
  $all+=$array3[array_rand($array3)];
}elseif(count($numbers)==3){
  $numbers[]=$array4[array_rand($array4)];
  $all+=$array4[array_rand($array4)];
}elseif(count($numbers)==4){
  $numbers[]=$array5[array_rand($array5)];
  $all+=$array5[array_rand($array5)];
}
if(count($numbers)==5 && $all!=484){
  $numbers=array();
  $all=0;
}
}

 

Fixed, changed the count($numbers)==4,5 supposed to be 3,4

Link to comment
https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266421
Share on other sites

unless... if you grab all 5 variables at the same time ::)

 

<?php
$array1=array("67","134","46","82","39");
$array2=array("88","165","50","79","190","146");
$array3=array("109","100","70","103","54","52","150","81");
$array4=array("61","118","55","97","112","138","85","124","73","127");
$array5=array("147","106","37","155","43","115","130");
while($all!=484){
$numbers[1]=$array1[array_rand($array1)];
$numbers[2]=$array2[array_rand($array2)];
$numbers[3]=$array3[array_rand($array3)];
$numbers[4]=$array4[array_rand($array4)];
$numbers[5]=$array5[array_rand($array5)];

$all=array_sum($numbers);
}
print_r($numbers);
?>

 

that'll prolly speed it up a bit...?

Link to comment
https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266426
Share on other sites

0.013 seconds

 

<?php
$time1 = microtime(true);

$a1=array("67","134","46","82","39");
$a2=array("88","165","50","79","190","146");
$a3=array("109","100","70","103","54","52","150","81");
$a4=array("61","118","55","97","112","138","85","124","73","127");
$a5=array("147","106","37","155","43","115","130");

$t1=count($a1);
$t2=count($a2);
$t3=count($a3);
$t4=count($a4);
for ($k1=0 ; $k1<$t1; $k1++) {
    for ($k2=0 ; $k2<$t2; $k2++) {
        for ($k3=0; $k3<$t3; $k3++) {
            for ($k4=0; $k4<$t4; $k4++) {
                $tot = $a4[$k4]+$a3[$k3]+$a2[$k2]+$a1[$k1];
                $rem = 484 - $tot; 
                if (in_array($rem, $a5)) 
                    echo "$a1[$k1], $a2[$k2], $a3[$k3], $a4[$k4], $rem <br>";
            }
        }
    }
}
$time2 = microtime(true);
echo '<br>Finished', ' in ', $time2-$time1, 'sec';
?>

 

EDIT: Can save a further 3 millisec by looping through a5 instead of a4 and testing if rem value is in a4, as a4 gives more iterations than a5. But who cares?

Link to comment
https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266459
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.