Jump to content

random generation of set numbers


mforan

Recommended Posts

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

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.

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";

?>

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";?>

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;
?>

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'];

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.