Page 1 of 2 12 LastLast
Results 1 to 10 of 13
  1. #1
    Pligg Donor dollars5's Avatar
    Joined
    Dec 2006
    Posts
    1,960
    Thanks
    Received:0
    Given: 0

    Arrow [SOLVED] Random Story Module Fix

    Hello,

    I just noticed that the random story module seemed to have some problem if there were some discarded stories.

    Example: Say you have just 10 stories, and 2-published and 8-discarded - then there is more probability that it would point to the discarded stories.

    Here is a small fix that will always guarantee to get a published story for reading:
    change
    Code:
    $randstory = rand(1, count($cols));
    to

    Code:
    $rand_num = rand(1, count($cols)-1);
    $randstory = $cols[$rand_num];

  2. #2
    Banned Geoserv's Avatar
    Joined
    Feb 2007
    Posts
    796
    Thanks
    Received:0
    Given: 0
    What file did you edit?

    Geoserv

  3. #3
    Pligg Donor dollars5's Avatar
    Joined
    Dec 2006
    Posts
    1,960
    Thanks
    Received:0
    Given: 0
    Oh, I forgot to mention the file /modules/random_story/random_story_main.php

  4. #4
    Pligg Developer/Admin kbeeveer46's Avatar
    Joined
    Jun 2006
    Posts
    3,215
    Thanks
    Received:1
    Given: 0
    Are you sure? If you take a look at the random story code it only grabs published stories.

    PHP Code:
    $cols $db->get_col('select link_id from ' table_links ' where `link_status` = "published" order by link_id desc limit 200;');
        
    //echo count($cols);
        
    if($cols){
            
    $randstory rand(1count($cols));
            
    $randstoryurl getmyurl("story"$randstory);
            
    $main_smarty->assign('random_story_randstoryurl'$randstoryurl);
        } 
    as you can see from the SQL query that it's only going to pull published stories from the database and then randomize those published stories. Maybe there is a bug with this but I'm not sure.

  5. #5
    Pligg Donor dollars5's Avatar
    Joined
    Dec 2006
    Posts
    1,960
    Thanks
    Received:0
    Given: 0
    A closer look will reveal that we only get the count of the published stories and find a random number - where there is some probability that the number can be an unpublished or discarded story even.
    Code:
    $randstory = rand(1, count($cols));
    Consider this small sample set:
    link_id : link_status
    1 : published
    2: discarded
    3 : queued
    4 : published
    5 : queued
    6: discarded
    7 : discarded
    8 : queued
    9 : published
    10 : discarded

    here published=3, queued=3, discarded=4
    the count($cols) = 3
    say rand(1,3) returns 2 - now it points to a discarded story.

    Though the original intention is to use only published stories, we rather seem to pick a random number of the count rather than a random published story.

    Thus the small modification of picking one of the 3 published story using the above tweak:
    Code:
    $randstory = $cols[rand(1, count($cols)-1)];
    Hope you get the point.

  6. #6
    Pligg Donor dollars5's Avatar
    Joined
    Dec 2006
    Posts
    1,960
    Thanks
    Received:0
    Given: 0
    Also since we consider
    count($cols)
    - it sometimes gets the last story+1 too so is the correction
    count($cols)-1)

  7. #7
    Pligg Developer/Admin kbeeveer46's Avatar
    Joined
    Jun 2006
    Posts
    3,215
    Thanks
    Received:1
    Given: 0
    I see what you mean. I'll have to double check with Ash about it.

  8. #8
    Pligg Donor dollars5's Avatar
    Joined
    Dec 2006
    Posts
    1,960
    Thanks
    Received:0
    Given: 0
    any updates on this.

  9. #9
    Casual Pligger sunstardude's Avatar
    Joined
    May 2007
    Posts
    30
    Thanks
    Received:0
    Given: 0

    It fixed it for me! Thanks!

    Quote Originally Posted by dollars5 View Post
    Hello,

    I just noticed that the random story module seemed to have some problem if there were some discarded stories.

    Example: Say you have just 10 stories, and 2-published and 8-discarded - then there is more probability that it would point to the discarded stories.

    Here is a small fix that will always guarantee to get a published story for reading:
    change
    Code:
    $randstory = rand(1, count($cols));
    to

    Code:
    $rand_num = rand(1, count($cols)-1);
    $randstory = $cols[$rand_num];
    Thanks, dollars5! Now what must be done to make it also include upcoming/queued stories when randomizing?

    Edit: Disregard this post... I just noticed it's NOT working. It seemed to be because only about 2 percent of the stories are discarded.

  10. #10
    Mayor of PliggVille/Coder AshDigg's Avatar
    Joined
    Dec 2005
    Posts
    1,515
    Thanks
    Received:0
    Given: 0
    Whoops!

    To fix it change this line
    PHP Code:
            $randstoryurl getmyurl("story"$randstory); 
    to this

    PHP Code:
            $randstoryurl getmyurl("story"$cols[$randstory]); 
    The problem is, say you have 1,000 stories. It will pick the last 200 stories, number 801 to 1000. It then picks a random number between 1 and 200, then shows the story with that ID. The problem is, the stories we want are ID 801 to 1000. So it's possible it'll pick story ID #56 (for example) which could be discarded or queued or maybe even deleted. The line above will now make it pick #56 from the results (801 + 56 = #857) and should now show the correct story.

Page 1 of 2 12 LastLast

Similar Threads

  1. Random story module
    By HitMan in forum Questions & Comments
    Replies: 4
    Last Post: 02-25-2011, 09:20 AM
  2. More efficient random story module
    By VLJ in forum Questions & Comments
    Replies: 1
    Last Post: 12-14-2007, 08:20 PM
  3. Random Story module needs quotes escaped
    By brianmark in forum Questions & Comments
    Replies: 1
    Last Post: 10-08-2007, 01:34 PM
  4. Replies: 1
    Last Post: 08-09-2007, 02:40 PM
  5. Random Story Module bug
    By floweringmind in forum Questions & Comments
    Replies: 0
    Last Post: 01-11-2007, 01:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Pligg Modules and Pligg Templates from Pligg Pro Web Hosting Services by Midphase Dreamhost Web Hosting Donate to Pligg