Jarod Posted February 26, 2010 Share Posted February 26, 2010 Okay in my script I am getting the same output sometimes (the $_GET['script'] and $random_num) that match each other. I don't want them to match, so I'm trying to fix this so that $random_num doesnt match $_GET['script']. private function getRandomScriptId() { $sql = mysql_query("SELECT id FROM script"); $maximum = mysql_num_rows($sql); $minimum = ($maximum-$maximum)+1; // starts at 1 $random_num = rand($minimum, $maximum); if($_GET['script'] == $random_num) { // I tried in here, but it didnt work either, I got a error sometimes new $random_num; } else { return $random_num; } } Link to comment https://forums.phpfreaks.com/topic/193450-how-do-i-generate-new-random-number-if-_getscript-matces-random_num/ Share on other sites More sharing options...
trq Posted February 26, 2010 Share Posted February 26, 2010 if($_GET['script'] == $random_num) { getRandomScriptId(); } else { return $random_num; } Link to comment https://forums.phpfreaks.com/topic/193450-how-do-i-generate-new-random-number-if-_getscript-matces-random_num/#findComment-1018454 Share on other sites More sharing options...
Jarod Posted February 26, 2010 Author Share Posted February 26, 2010 if($_GET['script'] == $random_num) { getRandomScriptId(); } else { return $random_num; } that did not work before, I tried that same exact thing the first time I tried to fix this problem (and you forgot to return it btw). Link to comment https://forums.phpfreaks.com/topic/193450-how-do-i-generate-new-random-number-if-_getscript-matces-random_num/#findComment-1018728 Share on other sites More sharing options...
ialsoagree Posted February 26, 2010 Share Posted February 26, 2010 Should it be $this->getRandomScriptId()? If that's not working, I suspect something else is wrong... Also, why do you have $minimum = ($maximum-$maximum)+1;? Anything minus itself is 0, and 0+1 is always 1... $minimum = 1; Actually, it's not even really necessary to set a variable at all... $random_num = rand(1, $maximum); Also, if you're just checking the number of results from a query, why select anything from the table at all? Just do a count, it will be much faster and won't waste memory. Where's $_GET['script'] defined? Link to comment https://forums.phpfreaks.com/topic/193450-how-do-i-generate-new-random-number-if-_getscript-matces-random_num/#findComment-1018731 Share on other sites More sharing options...
trq Posted February 26, 2010 Share Posted February 26, 2010 It will need to be prefixed with $this-> if the function is within a class & yeah, obviously it needs to be returned. Mistake on my part. It's always helpful to let us know what you have tried when posting a question on the board, were not mind readers. Link to comment https://forums.phpfreaks.com/topic/193450-how-do-i-generate-new-random-number-if-_getscript-matces-random_num/#findComment-1018737 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.