Jump to content

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


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.

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.