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

}
?>

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.