mforan Posted March 25, 2010 Share Posted March 25, 2010 Hi i want to select a random number (determined from allianceno), i did some research on the php documentation, array seems to be the best way about doing this, but it just throws out a 0. any ideas people? <?php $query = "SELECT draft,allianceno FROM alliances WHERE draft='1'"; $result = mysql_query($query) or die("Error: ".mysql_error()); $draft = array(); while ($row = mysql_fetch_array($result, MYSQL_NUM)) {$draft[] = $row;} $list .= "'0', "; for ($i = 0; $i < count($draft); $i++) { $list .= "'".$draft[$i][1]."',"; } echo $list ; $value = array_rand (array ($list)); echo "lol:$value"; ?> Link to comment https://forums.phpfreaks.com/topic/196435-random-generation-of-set-numbers/ Share on other sites More sharing options...
andrewgauger Posted March 25, 2010 Share Posted March 25, 2010 You don't need to parse the array into a string just to parse it back to an array. Use: $list[$i] = $draft[$i][1]; instead of: $list .= "'".$draft[$i][1]."',"; then replace $value = array_rand (array ($list)); with: $value = array_rand ($list); *untested* I also think there is a less resource intensive way to copy elements from a multi-dimensional array, but it escapes me. Link to comment https://forums.phpfreaks.com/topic/196435-random-generation-of-set-numbers/#findComment-1031384 Share on other sites More sharing options...
mforan Posted March 25, 2010 Author Share Posted March 25, 2010 is this what you mean? it doesnt work... <?php $query = "SELECT draft,allianceno FROM alliances WHERE draft='1'"; $result = mysql_query($query) or die("Error: ".mysql_error()); $draft = array(); while ($row = mysql_fetch_array($result, MYSQL_NUM)) {$draft[] = $row;} for ($i = 0; $i < count($draft); $i++) { $list[$i] = $draft[$i][1]; } $value = array_rand ($list[$i]); echo "lol:$value"; ?> Link to comment https://forums.phpfreaks.com/topic/196435-random-generation-of-set-numbers/#findComment-1031388 Share on other sites More sharing options...
andrewgauger Posted March 25, 2010 Share Posted March 25, 2010 Nope, take the $i out of the line: $value= array_rand($list); Link to comment https://forums.phpfreaks.com/topic/196435-random-generation-of-set-numbers/#findComment-1031412 Share on other sites More sharing options...
mforan Posted March 25, 2010 Author Share Posted March 25, 2010 This works, but one of the values (4) that i am trying to exclude, is being shown up. I want a random pattern for the numbers: 0 1 2 3 5 7 9 10 12 14 (but the numbers that are missing can change) <?php $query = "SELECT draft,allianceno FROM alliances WHERE draft='1'"; $result = mysql_query($query) or die("Error: ".mysql_error()); $draft = array(); while ($row = mysql_fetch_array($result, MYSQL_NUM)) {$draft[] = $row;} for ($i = 0; $i < count($draft); $i++) { $list[$i] = $draft[$i][1];} $value = array_rand ($list); echo "lol:$value";?> Link to comment https://forums.phpfreaks.com/topic/196435-random-generation-of-set-numbers/#findComment-1031665 Share on other sites More sharing options...
andrewgauger Posted March 25, 2010 Share Posted March 25, 2010 Oh, $value= array_rand($list,count($list)); ? Link to comment https://forums.phpfreaks.com/topic/196435-random-generation-of-set-numbers/#findComment-1031696 Share on other sites More sharing options...
mforan Posted March 25, 2010 Author Share Posted March 25, 2010 that outputs lol:Array i found a solution to the problem thou: <?php $query = "SELECT allianceno FROM alliances WHERE draft=1"; $result = mysql_query($query) or die("Error: ".mysql_error()); $draf = array(); while ($row = mysql_fetch_array($result, MYSQL_NUM)) {$draf[] = $row[0];} while (($count < count($draf)) { $random = mt_rand(1, count($draf)); $randomalliance = $draf[$random]; $count++; } echo $randomalliance; ?> Link to comment https://forums.phpfreaks.com/topic/196435-random-generation-of-set-numbers/#findComment-1031733 Share on other sites More sharing options...
shlumph Posted March 25, 2010 Share Posted March 25, 2010 Seems like overkill to get a single random alliance number. Why not just do something like this? $query = "SELECT allianceno FROM alliances WHERE draft=1 ORDER BY RAND() LIMIT 1"; $result = mysql_query($query) or die("Error: ".mysql_error()); $data = mysql_fetch_array($result, MYSQL_ASSOC); echo $data['allianceno']; Link to comment https://forums.phpfreaks.com/topic/196435-random-generation-of-set-numbers/#findComment-1031741 Share on other sites More sharing options...
mforan Posted March 25, 2010 Author Share Posted March 25, 2010 Of course, why didnt i think of that 1. mysql has its own random generator. Link to comment https://forums.phpfreaks.com/topic/196435-random-generation-of-set-numbers/#findComment-1031761 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.