Jump to content

Randomly Pick, but not pick two of the same?


vexious

Recommended Posts

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

}
?>

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.

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.