vexious Posted December 2, 2007 Share Posted December 2, 2007 So i've started out with this so far, how/what changes can i make so that it wont pick two of the same though.. I just can't seem to think of a way.. Thanks <?php $chars = ""; $chars = file('feedlist.txt'); function random_subarray($ary, $len, $key) { srand($key); shuffle($ary); return array_slice($ary, 0, $len); } # example srand((double)microtime()*1000000); $code = rand(0,100); $fred = (random_subarray($chars, 5, $code)); foreach ($fred as $key) { $count++; echo " <a href='d'>$key</a><br /> "; } ?> Link to comment https://forums.phpfreaks.com/topic/79870-randomly-pick-but-not-pick-two-of-the-same/ Share on other sites More sharing options...
sKunKbad Posted December 2, 2007 Share Posted December 2, 2007 Member "sasa" come up with this a while back. It randomizes the choices, and doesn't pick the same choice twice unless the array of choices has been depleted. sasa_array.inc <?php $a_text_files = array( 'file_a.txt', 'file_b.txt' , 'file_c.txt', 'file_d.txt', 'file_e.txt' ); ?> sasa_rand.php <?php session_start(); include('sasa_array.inc'); if (!array_key_exists('rand_text_files', $_SESSION)) $_SESSION['rand_text_files'] = $a_text_files; if (count($_SESSION['rand_text_files']) == 0) $_SESSION['rand_text_files'] = $a_text_files; shuffle($_SESSION['rand_text_files']); $rand_text = $_SESSION['rand_text_files'][0]; unset($_SESSION['rand_text_files'][0]); $handle = file($rand_text); foreach ($handle as $line_num => $line) { echo $line; } ?> You could either use this, or modify it to suit your needs. Link to comment https://forums.phpfreaks.com/topic/79870-randomly-pick-but-not-pick-two-of-the-same/#findComment-404487 Share on other sites More sharing options...
ToonMariner Posted December 2, 2007 Share Posted December 2, 2007 if all the values of your array are unique then a simple array_rand($arr, 2) will suffice. you can make the array elements unique by using array_unique($arr) Link to comment https://forums.phpfreaks.com/topic/79870-randomly-pick-but-not-pick-two-of-the-same/#findComment-404538 Share on other sites More sharing options...
Crew-Portal Posted December 3, 2007 Share Posted December 3, 2007 sKunKbad Sorry for using this thread. But i was just wondering how you can make your PHP code all colorful outside a [ code ] box? Link to comment https://forums.phpfreaks.com/topic/79870-randomly-pick-but-not-pick-two-of-the-same/#findComment-404629 Share on other sites More sharing options...
phpQuestioner Posted December 3, 2007 Share Posted December 3, 2007 sKunKbad Sorry for using this thread. But i was just wondering how you can make your PHP code all colorful outside a [ code ] box? try the "Change Color" select menu in thread posting tools Link to comment https://forums.phpfreaks.com/topic/79870-randomly-pick-but-not-pick-two-of-the-same/#findComment-404630 Share on other sites More sharing options...
sKunKbad Posted December 3, 2007 Share Posted December 3, 2007 sKunKbad Sorry for using this thread. But i was just wondering how you can make your PHP code all colorful outside a [ code ] box? just wrap your code in php bbcode tags Link to comment https://forums.phpfreaks.com/topic/79870-randomly-pick-but-not-pick-two-of-the-same/#findComment-404887 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.