PHP - Array_rand() Not Really Randomizing.
Although I found a way around it, I want to know, logically, why the behavior is so.
// words.txt is a text file with dictionary words on each line $lines = file('words.txt'); // The goal is to pick two random words, 10 times. for($i = 0; $i < 10; $i++) { #shuffle($lines); <--- un-commenting this is the fix i found. $words = array_rand($lines, 2); echo "Words: " . $lines[$words[0]] . ", " . $lines[$words[1]] . "<br>"; } If i don't have shuffle() in there, I get two different set of words, not 10. ie (result from code above) Words: massacre , spear Words: frontiersman , kumquat Words: massacre , spear Words: frontiersman , kumquat Words: massacre , spear Words: frontiersman , kumquat Words: massacre , spear Words: frontiersman , kumquat Words: massacre , spear Words: frontiersman , kumquat Why is this? Similar TutorialsHello All, I'm trying to work out how to use array_rand() to output a random value in an array. So i've tested it in a single array, and it works fine: $input1 = array("Link1", "Link2", "Link3", "Link4", "Link5"); $rand_keys = array_rand($input1, 2); echo $input1[$rand_keys[0]] . "\n"; However, I want 'Link1', 'Link2' etc to have their own SPECIFIC URL values, so i've tried declaring another array: $input1 = array("Link1", "Link2", "Link3", "Link4", "Link5"); $input2 = array('page.php','file.php','ends.php','smile.php'); //So 'Link1 = page.php, Link2 = file.php etc... $rand_keys = array_rand($input1, 2); echo 'The value for input 1 is: '.$input1[$rand_keys[0]] . 'and the url is '.??????????????.' "\n"; But can't work out how i'd grab this value - anyone kindly help? Hey guys, quick question. The array_rand() function. How random is it? I'm building an app in codeigniter where i'd need to return 2/6 keys from an array and store them in a DB. I don't want it to return a key that has already been returned. I'm sure logically there's always a chance that I could get back something that has already been returned. Also i'm using the function at the same time, so it isn't over any period of time or within different methods. Are there any other recommendations apart from array_rand() ? Thanks! Hi, I have a function that pulls a random line from a text file and I would like to change it so it pulls a random line from a text file that contains a certain date, heres the current function : Code: [Select] function RandomLine($filename) { $lines = file($filename) ; return $lines[array_rand($lines)] ; } Any ideas on the best way to do this ? Many thanks. Hi Guys I was just wondering if their is a function to randomize an array pulled from database is it possible i know their is array_rand() but it doesnt seem to work for me can someone show me where i am going wrong or do i have to do it from the actual SELECT statemant i.e ORDER_RAND(); ? Here is what my array prints out. $user_list = get_users_by_specialism($specialism); print_r($user_list); outputs. Array ( [0] => 129 [1] => 46 [2] => 57 [3] => 109 [4] => 92 [5] => 137 [6] => 238 [7] => 101 [8] => 60 [9] => 90 [10] => 112 [11] => 133 [12] => 121 [13] => 220 [14] => 275 [15] => 278 ) I basically need to make this array output in a random order everytime not by ID incrementing. Any help Please Cheers. I'm currently working on a browser based mmorpg. First things first, here is an example of my battle.php <?php include_once 'connect.php'; session_start(); include_once 'logo.php'; ?> <link href="style.css" rel="stylesheet" type="text/css" /> <div id="login2" div align="center"> <?php if (isset($_SESSION['player'])) { $player=$_SESSION['player']; } else { echo "Not Logged in <br><br> <A href='login.php'>Login</a>"; exit; } ?> </div> <?php $playerinfo="SELECT * from players where name='$player'"; $playerinfo2=mysql_query($playerinfo) or die("could not get player stats!"); $playerinfo3=mysql_fetch_array($playerinfo2); include_once 'statpanel.php'; $pid = $playerinfo3['id']; $name = $playerinfo3['name']; $playerhp = $playerinfo3['hpoints']; $playerattack = $playerinfo3['attack']; $playerdefense = $playerinfo3['defense']; if ($playerhp < 1) { echo "You are dead!" ; echo "<br><a href='inn.php>Heal at the lake"; exit; } ?> <div id="table"> <?php if (isset($_GET['randid'])) { $randid=$_GET['randid']; $iteminfo="SELECT * from inventory where randid='$randid' AND id ='$pid'"; $iteminfo2=mysql_query($iteminfo) or die("could not get item stats!"); $iteminfo3=mysql_fetch_array($iteminfo2); if (!$iteminfo3['name']) { } else { $iname = $iteminfo3['name']; $stats = $iteminfo3['stats']; $statadd = $iteminfo3['statadd']; $type = $iteminfo3['type']; if ($type == "healing") { $newhp = $statadd + $playerhp; if ($newhp > $playerinfo3['maxhp']) { $newhp = $playerinfo3['maxhp']; } $updateplayer="update players set hpoints='$newhp' where id='$pid'"; mysql_query($updateplayer) or die("Could not update player"); $updateitem="DELETE from inventory where id='$pid' AND randid='$randid' limit 1"; mysql_query($updateitem) or die("Could not delete item"); $playerhp = $newhp; echo "Used " . $iname . " and recovered " . $statadd . ".<br>"; } }} $creature = $playerinfo3['creature']; if ($creature != 0) { $creatureinfo="SELECT * from creatures where id='$creature'"; $creatureinfo2=mysql_query($creatureinfo) or die("could not get the creature you were fighting!"); $creatureinfo3=mysql_fetch_array($creatureinfo2); } else { $creatureinfo="SELECT * from creatures order by rand() limit 1"; $creatureinfo2=mysql_query($creatureinfo) or die("could get a creature!"); $creatureinfo3=mysql_fetch_array($creatureinfo2); $cid = $creatureinfo3['id']; $updateplayer="update players set creature='$cid' where name='$name'"; mysql_query($updateplayer) or die("Could not update player"); } $creature = $creatureinfo3['name']; $creaturehp = $creatureinfo3['hpoints']; $creatureattack = $creatureinfo3['attack']; $creaturedefense = $creatureinfo3['defense']; ?> </div> <div id="player"> <?php /////player info echo "<u> " . $playerinfo3['name'] . "</u><br>"; echo "Hit points = " . $playerhp . "<br>"; echo "Attack = " . $playerattack . "<br>"; echo "Defense = " . $playerdefense . "<br><br><br>"; ?> </div> <div id="creature"> <img src="images/<?php echo $creatureinfo3['imgurl'];?>" <br> <?php ///////creature info echo "<u> " . $creatureinfo3['name'] . "</u><br>"; echo "Hit points = " . $creaturehp . "<br>"; echo "Attack = " . $creatureattack . "<br>"; echo "Defense = " . $creaturedefense . "<br><br><br>"; ?> </div> <div id="options"> <?php echo "<a href='attack.php'>Attack</a>"; echo "<br><a href='usemagic.php'>Use Magic</a>"; echo "<br><a href='useitem.php'>Use Item</a>"; echo "<br><a href='index.php'>Exit Arena</a>"; ?> </div> <div id="logout"> <?php echo "<br><a href='logout.php'><img src='images/logout.gif'>"; ?> </div> Particular functions I'm interested in for zone creation. -Pay to get in & have the initial icon switch to a now usable adventure zone. (done) -When 'adventuring' in this link. Battle php randomizes encounters between 'combat encounters' and 'non-combat' encounters. (can't get this right, and removed my utter failures from the code above as to not clutter it. Pulls randomly from the Creature DB just fine but when I try to add in the 2nd,non-combat,db..it pulls nothing.) -NonCombat encounters dish our reward. (currently my non combats do not give reward. Unlike a standard creature encounter where a players gold and exper get updated on a victory...I dont know how to immediately trigger the change on encounter & I'm epically unsure how to craft the $updateplayer= to be so broad as to include all possibilities of improvement but only to utilize the one being called upon in the encounter) -I'm also interested in updating my battle.php to include a dropdown list of usable spells/items instead of the current method of Attacking/going back/selecting magic or items/going back/...repeat. It works, sure, but its nasty..Any ideas on how to call on the database for user specific data to fill in a drop down menu? I've used this before and it worked fine but the mt_rand is not working properly. It keeps producing the same number. I've tested it on with the $_GET to the next page and it produces a unique code but it doesn't want to insert that number into the table, just the other same, repetitive number. $transaction_id = mt_rand() . mt_rand(); $item_email = mysql_real_escape_string($_POST[email]); $item_first_name = mysql_real_escape_string($_POST[first_name]); $item_last_name = mysql_real_escape_string($_POST[last_name]); $item_street = mysql_real_escape_string($_POST[street]); $item_street2 = mysql_real_escape_string($_POST[street_2]); $item_city = mysql_real_escape_string($_POST[city]); $item_state = mysql_real_escape_string($_POST[state]); $item_zip_code = mysql_real_escape_string($_POST[zip_code]); $item_country = mysql_real_escape_string($_POST[country]); $username = $_SESSION['username']; $item_id = $_GET['id']; $item_title = mysql_real_escape_string($_POST[title]); $item_price = mysql_real_escape_string($_POST[price]); $item_shipping_cost = mysql_real_escape_string($_POST[shipping_cost]); $item_in_shipping = mysql_real_escape_string($_POST[in_shipping]); $okay = TRUE; if ($item_country == 'United States'){ $shipping = "$item_shipping_cost"; } else { $shipping = "$item_in_shipping - <span class=\"submissionfont\">International Rate</span>"; } $sql="INSERT INTO buyer (username, email, first_name, last_name, street, street_2, city, state, zip_code, country, transaction_id, date) VALUES ('$username', '$item_email', '$item_first_name', '$item_last_name', '$item_street', '$item_street2', '$item_city', '$item_state', '$item_zip_code', '$item_country', '$transaction_id', CURDATE())"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } $sql2="UPDATE product SET transaction_id='$transaction_id', sold='1', sold_date=CURDATE() WHERE id = '$item_id'"; if (!mysql_query($sql2)) { die('Error: ' . mysql_error()); } |