Click to See Complete Forum and Search --> : how to print random data?


learner2006
03-06-2007, 08:32 PM
HEllo,
I am stuck in making a code work, and I need some help,
if you can help, it is appreciated,

this is a simple count code, it count some data from a file called data.csv

then it will print one line of data when the script is called,
it works fine and not a problem,

the only issue, because after printing is done the code will be sets to zero, the next time when script is called, the data will be from the beginning of the data.csv file, so always the data that is pulled and printed are same,

data.csv contains lots of data, which seperated by %%
I want all the data be printed ( not all at once) and when there is no data left then goes to the first one, which i think there is a need for memory or something,

or at least make it random, and make sure all data will be printed.

it does not matter which way,


here is part of the code that works:


$count = $oldcount = $totalcount = $tally = 0;
$data = file_get_contents("data.csv");
$data = explode('%%', $data);
$di = 0;


$printme .= "\n" . $data[$di++];
if($di>=count($data))$di=0;
$tally = 0;
}
}

$totalcount += $count;
}
}

echo $printme;
}
?>

appreciate your help

pcthug
03-06-2007, 09:26 PM
I'm not exactly sure if I understood your question, but try shuffling your array of data: $data = shuffle(explode('%%', $data));

learner2006
03-06-2007, 09:40 PM
thanks for the reply,

I replaced that line, but now it does not echo anything,

pcthug
03-07-2007, 12:28 AM
Sorry, forgot the shuffle function accepts the argument by reference rather than returning a new value. Try this: $data = explode('%%', $data);

shuffle($data);

learner2006
03-07-2007, 12:57 AM
Thanks for the reply,

yes it works now, random data.

and when the page is refershed the data will be changed too,
there is not a problem on this. it is OK.

Thanks a lot.

NightShift58
03-07-2007, 08:49 AM
php]$data = explode('%%', $data);
shuffle($data);[/php]Can I suggest the use of rand() instead. It seems that shuffling a (potentially) large array is overkill.<?php
$data = explode('%%', $data);
$rand_pos = rand(0, count($data)-1);
echo $data[$rand_pos];
?>Shuffling would be fine if we wanted to rearrange the order of things and then run through the entire array in that random order. But to just pick out one element at random, shuffle() would be my second choice.

A shortcoming to both methods, however, is that we have no guarantee that all the data will eventually be printed, as originally mentioned. For that to happen, a little bit more work would be required.

pcthug
03-07-2007, 05:59 PM
I was unsure about the original question, but if the OP only wanted to display 1 array value I would suggest using the mt_rand function over the rand function. It's four times faster, generates a more-random value and is less intense on system resources.

But there is no need to reinvent the wheel, there is already a specific function that picks one or more random entries out of an array; array_rand() (http://www.php.net/manual/en/function.array-rand.php)


$data = explode('%%', $data);
echo array_rand($data);

NightShift58
03-07-2007, 06:15 PM
I can more than live with that...