Jump to content

Not random enough,know how to improve this random play code ?


oli22

Recommended Posts

I am using this code to play random videos from a folder, i have many videos but somehow the same ones are chosen too often, i don't know why... Would you know how to make it more random, so a wider variety of videos gets played???

Could it be because of the titles somehow( titles are not numbers-->titles  are characters)?

Would a random formula using mysql/tables be better?

 

<?php

function getRandomFile($path)

{

    $files = array();

 

    foreach( new DirectoryIterator($path) as $file )

    {

        if($file->isFile())

        {

            $files[] = $path . $file->getFilename();

        }

    }

 

    $i = array_rand($files);

    return $files[$i];

}

 

$path = "vids";

$url = 'http://127.0.0.1/randomPlay/';

$file = getRandomFile($path);

 

//$final= str_replace('vids', $path, $file);

//echo ($final)

$final= substr($file, 4);

//echo ($final);

?>

 

Videos are in the "vids" folder

 

I don't believe in random. :P

 

But... a suggestion is to create a session that stores selected videos. You could then come up with some kind of formula to determine when a video is chosen based on if it has already been shown or not.

Hi,thanks

 

I know some play more often because i refreshed the page many times to see the results and out of a bank of about 25 videos, the same 3 re-played over way too many times, bad random.

 

a suggestion is to create a session that stores selected videos. You could then come up with some kind of formula to determine when a video is chosen based on if it has already been shown or not.

 

I did have a thought about that but i am a real newbee so  can someone tell me further how to do this?

This random video thing happens automaticaly when people arrive on the web page. There is no login...

 

Hi,thanks

 

I know some play more often because i refreshed the page many times to see the results and out of a bank of about 25 videos, the same 3 re-played over way too many times, bad random.

 

Sorry,but until you actually have measured that over several hundreds page loads, I'll remain unconvinced.

Chmpdog, that random info is a great demonstration.I wonder if they have a code i can use, i will browse them. Otherwise i guess i will try to make a php code to memorize and create a filter to even out most played, work in progress...

 

 

  • 1 month later...

Chmpdog, that random info is a great demonstration.I wonder if they have a code i can use, i will browse them. Otherwise i guess i will try to make a php code to memorize and create a filter to even out most played, work in progress...

 

 

From what I can tell, the random.org site is using the Mersenne Twister method of getting a random number.  PHP has a Mersenne Twister random function called mt_rand and you can find info on that function at php.net/mt_rand.

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.