bluefrog Posted December 22, 2015 Share Posted December 22, 2015 I am trying to write what should be a short piece of code to generate a 4th number based on three. There is an array with 6 numbers in it, the code needs to take the last three (numerical order) and generate a fourth based on some rules: if last number (array [5]) = the same as array[4] and array[5] is less than 49 then array[5] = array[5] +1 if last number (array [5]) = the same as array[4] and array[5] is equal to 49 then array[5] = array[4] -1 IF array[3] is more than 2 less than array[4] eg: Number 1,2,44,45,47,48 By these rules the new number can be 49, as although number number 5[4] would be the same as the generated number ([5]-1) it is less than 49 so can be number 6[5] +1 instead. Number 1,2,44,45,46,48 By these rules the generated number can be 47, as number 5[4] is more than two away from the last number [5] Number 1,2,44,45,48,49 By these rules the generated number would be 47, as the last number [5] is 49 and the second last [4] is the next number in reverse sequence, so cannot go higher than 49 and can't use the next number down, so uses the next number [4] -1 Here is what I've cobbled together - no laughing please I'm new: <?php //$lunch = explode(",",$lt_results); $lunch = array("1","1","1","44","47","48"); sort($lunch); echo '<br>'; echo $lunch[3]; echo ' - '.$lunch[4]; echo ' - '.$lunch[5]; $last = --$lunch[5]; echo '<br>Last = '.$last; if ($last == $lunch[4]){ echo "<br>Ball 5 & pick are the same"; if ($lunch[5]<"49"){ $last = $lunch[5]; echo $last; } } else { echo "a is smaller than b"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/299959-auto-generate-a-number-based-on-three-from-an-array-with-rules/ Share on other sites More sharing options...
benanamen Posted December 22, 2015 Share Posted December 22, 2015 To avoid the X Y Problem could you please provide an overview of the problem you are trying to solve. (I dont mean an explanation of how you think you need to do something.) Quote Link to comment https://forums.phpfreaks.com/topic/299959-auto-generate-a-number-based-on-three-from-an-array-with-rules/#findComment-1528459 Share on other sites More sharing options...
bluefrog Posted December 22, 2015 Author Share Posted December 22, 2015 (edited) The array is 6 numbers, I want to take the highest three numbers and generate a fourth number based on the highest number (one less). However if the highest number is 48 and the next highest is 47, then the generated number should be 49, but no higher. If the two numbers were 48 and 49 then the number should be lower, so in this case 47 (one less than the next lowest. Works on numbers 1-49. This is for a lottery website I am setting up (hopefully). Does that make sense? It does in my head lol Edited December 22, 2015 by bluefrog Quote Link to comment https://forums.phpfreaks.com/topic/299959-auto-generate-a-number-based-on-three-from-an-array-with-rules/#findComment-1528466 Share on other sites More sharing options...
benanamen Posted December 22, 2015 Share Posted December 22, 2015 (edited) ` Edited December 22, 2015 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/299959-auto-generate-a-number-based-on-three-from-an-array-with-rules/#findComment-1528467 Share on other sites More sharing options...
bluefrog Posted December 22, 2015 Author Share Posted December 22, 2015 (edited) That is what I'm trying to achieve, generate a number based on three others. Or do you mean the 'purpose' of the code. Oh you've edited your post to a "." Guess this makes no sense to people reading it now lol Edited December 22, 2015 by bluefrog Quote Link to comment https://forums.phpfreaks.com/topic/299959-auto-generate-a-number-based-on-three-from-an-array-with-rules/#findComment-1528468 Share on other sites More sharing options...
benanamen Posted December 22, 2015 Share Posted December 22, 2015 (edited) Yes, the purpose. The process as seen from outside the code would be helpful. LIke:User clicks button and based on code calculation X number of numbers are displayed to user...... * Where does this array come from? Edited December 22, 2015 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/299959-auto-generate-a-number-based-on-three-from-an-array-with-rules/#findComment-1528469 Share on other sites More sharing options...
Psycho Posted December 22, 2015 Share Posted December 22, 2015 (edited) I'm confused on your explanation. You keep stating you want to use the "highest" three numbers to generate a fourth. But, all of your examples just happen to have the highest (largest) numbers in the last position. So, it is not clear if you are talking about the three numbers with the greatest value or the ones in the last positions of the array. Also, you keep stating the generated value should be based on three numbers, but the logic you have presented only seems to use two. EDIT: Also your explanation of the rules to determine the number are a mess. You state rules when the 4th and 5th numbers are equal to each other, but then give examples where they are not equal to each other. I would think that for a lottery they would never be equal. Edited December 22, 2015 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/299959-auto-generate-a-number-based-on-three-from-an-array-with-rules/#findComment-1528472 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.