jonsamwell Posted January 25, 2009 Share Posted January 25, 2009 Hi all, This isn't so much a php question but a general coding how to. Basicallly i'm trying to generate a 20 character key where each char corresponds to a value. however, i want the total value to = 500. I've got this code so far but it just gets stuck in an infinite loop. Any ideas how i can make it work?? <?php class KeyGen { //Length of key private $keyLength; //Actual Key private $Activation_Code = ""; //Final Value key must equal private $Activation_Code_Sum = 500; //Key Elements private $AlphaNumerics = array("A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "R", "S", "T", "W", "X", "Y", "Z", 1, 2, 3, 4, 5, 6, 7, 8, 9); //Corrosponding Key Element Values private $AlphaNumerics_Values = array( 22, 96, 13, 1, 3, 31, 19, 5, 2, 18, 12, 65, 26, 99, 15, 11, 4, 25, 17, 73, 5, 88, 50, 9, 8, 7, 65, 37, 22, 40); public function __construct($keyLen) {//Set Length of Key NOTE would always be 20 $this-> keyLength = $keyLen; } public function generateKey() {//Generate Key - Key value must equal $Activation_Code_Sum //current value of the key $currentValue = 0; //Generate random starting char and add to key //Get random starting character $j = rand(0, count($this->AlphaNumerics)); //Add char to code $this->Activation_Code .= $this->AlphaNumerics($j); //Increase current value $currentValue += $this->AlphaNumerics_Values($j); //Loop round so key has specified number of chars for ($i = 0; $i < $this->keyLength -1; $i++) { //Get random num $j = rand(0, count($this->AlphaNumerics)); //Determine next value $nextValue = $this->AlphaNumerics_Values($j); /*Check nextValue is not greater than current difference * between actual key value and final key value*/ while ($nextValue > ( $this->Activation_Code_Sum - $currentValue ) ) {//If so generate new random number and get ne value //TODO: Should make this seperate function and recurse //Get random num $j = rand(0, count($this->AlphaNumerics)); $nextValue = $this->AlphaNumerics_Values($j); } $this->Activation_Code .= $this->AlphaNumerics($j); //Increase current value $currentValue += $this->AlphaNumerics_Values($j); } return $this-> Activation_Code; } } ?> Any tips would be great!!! Thanks Jon Link to comment https://forums.phpfreaks.com/topic/142338-key-generator/ Share on other sites More sharing options...
DarkSuperHero Posted January 25, 2009 Share Posted January 25, 2009 <?php class KeyGen { //Length of key private $keyLength; //Actual Key private $Activation_Code = ""; //Final Value key must equal private $Activation_Code_Sum = 500; //Key Elements private $AlphaNumerics = array("A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "R", "S", "T", "W", "X", "Y", "Z", 1, 2, 3, 4, 5, 6, 7, 8, 9); //Corrosponding Key Element Values private $AlphaNumerics_Values = array( 22, 96, 13, 1, 3, 31, 19, 5, 2, 18, 12, 65, 26, 99, 15, 11, 4, 25, 17, 73, 5, 88, 50, 9, 8, 7, 65, 37, 22, 40); public function __construct($keyLen) {//Set Length of Key NOTE would always be 20 $this->keyLength = $keyLen; //space betweeen $this and keyLength } public function generateKey() {//Generate Key - Key value must equal $Activation_Code_Sum //current value of the key $currentValue = 0; //Generate random starting char and add to key //Get random starting character $j = rand(0, count($this->AlphaNumerics)); //Add char to code $this->Activation_Code .= $this->AlphaNumerics[$j]; //use brakets not prenthesis //Increase current value $currentValue += $this->AlphaNumerics_Values[$j]; //use brakets not prenthesis //Loop round so key has specified number of chars for ($i = 0; $i < $this->keyLength -1; $i++) { //Get random num $j = rand(0, count($this->AlphaNumerics)); //Determine next value $nextValue = $this->AlphaNumerics_Values[$j];//use brakets not prenthesis /*Check nextValue is not greater than current difference * between actual key value and final key value*/ while ($nextValue > ( $this->Activation_Code_Sum - $currentValue ) ) {//If so generate new random number and get ne value //TODO: Should make this seperate function and recurse //Get random num $j = rand(0, count($this->AlphaNumerics)); $nextValue = $this->AlphaNumerics_Values[$j];//use brakets not prenthesis } $this->Activation_Code .= $this->AlphaNumerics[$j]; //use brakets not prenthesis //Increase current value $currentValue += $this->AlphaNumerics_Values[$j]; //use brakets not prenthesis } return $this->Activation_Code;//space betweeen $this and Activation_Code } } $n = new KeyGen(20); echo $n->generateKey(); ?> i edited your code, and i was able to get it to generate a key, i place comments next to the stuff i changed.... Link to comment https://forums.phpfreaks.com/topic/142338-key-generator/#findComment-745806 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.