$z=355;// it is the answer of our problem and it was given to us.
$x = mt_rand(1, 1000000); //Generate $x $y = $x + $z;// we got $y. We are done :)
$y-$x = 355;
This simple thinking has helped me in implementing several random effects I had been working on with apps and products I have been building for the past months.I have actually found it’s mutations really handy in several locations.
Think starting from the simplest you can think of, build, then go to which ever complicated level you want.
Remember “Everything should be made as simple as possible, not simpler“-Albert Einstein.
<?php /** * Description : Function to generate two random numbers so that the difference is always 355. * * @author Njie Litumbe .L. Nara <njielitumbe@gmail.com><njielitumbe.blogspot.com> * @Under Nason Systems, Inc. <www.nasaonsystems.com> * * All rights Open. */ class TwoRand{ var $a ; var $b ; function tworand(){ $this->a =1;//just the variables, making it clean $this->b = 1000000; $x=mt_rand($this->a,$this->b);//Generate $x $y= mt_rand($this->a,$this->b);//Generate $y (Please don't use arithmetic) //Logic //proceed, it can be $y-$x, we just want to make sure it is positive before they do any thing if(($x -$y >0)) { //lets go recursive while(($x - $y)!== 355) { $this->tworand(); exit; } echo "Helloo, We got it! "; $diff= $x-$y; echo 'X = '.$x.' Y= '.$y; echo "Z(Difference)". $diff; return 0; } else { $this->tworand(); } } }//end of class tworand $tworand = new TwoRand();
Our 3 lines of code has ended up as 30 lines including undetermined recursive-ness.
Character of the Above code;
- Inefficient:
After some undetermined number of refresh before you can actually get the results spitting to the browser, for the other times you get:
- Not an Algorithm
Undetermined growth rate.
Hardly satisfy any of the conditions for an algorithm
- Just thought up on impulse to satisfy my Arithmetic hater friends 🙂 they know themselves :).
I may not find time to figure out the actual algorithm to to satisfy my Arithmetic hater friends :).
It is open for they or anyone to suggest something different.