phprandomratingfaker

PHP fake average rating generator


Let's imagine that the material has a rating of 2.7 with 7 votes. I want to create a script that would generate my desired rating that would look real and not fake.

For example, I want the rating to change from 2.7 to 4.6, but the number of votes should also change proportionally. I can't figure out how to implement it correctly. My script will make the rating 6.0 as I want, but as a result the number of votes will be as high as 500+. I want to refine the script so that it is more realistic and changes the rating with minimal votes My code:

My script will make the rating 6.0 as I want for example, but the number of votes will be as high as 500+. I want to generate the average values of the rating based on the previous values, while the deviation in the votes should not look fake.

<?php

class RatingFaker
{
    private $totalRating = 10; // Maximum rating threshold (5 or 10)
    private $maxRatingGenerate = 10; // We generate maximum rating values during the passage
    private $minRatingGenerarate = 5; // Minimally generates a rating value during a pass
    private $minRatingCheck = 3.5; // The minimum acceptable threshold from which we start to act
    
    public function __construct($minRatingCheck)
    {
        $this->minRatingCheck = $minRatingCheck;
    }
    
    private function calcAverageRating($ratings = []) 
    {
        $totalWeight = 0;
        $totalReviews = 0;
    
        foreach ($ratings as $weight => $numberofReviews) {
            $WeightMultipliedByNumber = $weight * $numberofReviews;
            $totalWeight += $WeightMultipliedByNumber;
            $totalReviews += $numberofReviews;
            }
    
        //divide the total weight by total number of reviews
        $averageRating = $totalWeight / $totalReviews;
    
        return [
            'rating' => $averageRating,
            'vote_num' => $totalReviews
        ];
    }
    
    private function getRandVoters()
    {
        $randRating = rand($this->minRatingGenerarate, $this->maxRatingGenerate);
        $randVoters = rand(1, 99);
        
        return [
            $randRating => $randVoters
        ];
    }
    
    private function ratingLoop($valueList)
    {
        $valueList = array_merge($valueList, $this->getRandVoters());
        
        $newRating = $this->calcAverageRating($valueList);
        
        if($newRating['rating'] < $this->minRatingCheck) {
            $valueList = array_merge($valueList, $this->getRandVoters());
            
            return $this->ratingLoop($valueList);
        }
        
        if($newRating['rating'] > 10) {
            $newRating['rating'] = 10;
        }
        
        return [
            'rating' => round($newRating['rating'], 1),
            'sum' => round($newRating['vote_num'] * round($newRating['rating'], 1)),
            'voters' => $newRating['vote_num']
        ];
    }
    
    public function check($currentRate, $currentVoteNum)
    {
        if($currentRate < $this->minRatingCheck) {
            $rating = $this->ratingLoop([
                $currentRate => $currentVoteNum,
            ]);
        }
        
        return $rating ?? [];
    }
}

$currentRate = 2.4;
$voteNum = 88;
$oldRating = [
    'rating' => $currentRate,
    'sum' => round($voteNum * $currentRate),
    'voters' => $voteNum
];
$rating = [];

$ratingFaker = new RatingFaker(6.0);
$rating = $ratingFaker->check($currentRate, $voteNum);

echo '<pre>';
echo 'Was:';
print_r($oldRating);
echo "<br>";
echo "<br>";
echo "<br>";
echo 'Now:';
print_r($rating);
echo '</pre>';

Solution

  • When the current rating is 2.5 based on 9 votes, and you want to get a rating of 6.0 with a minimal number of votes, you need to make sure the total value of 10 votes is 60 ( 60/10 = 6.0 ).

    The current rating is : ( 2.5*9 / 9 ) = 2.5

    With you extra vote it is: ( (2.5*9+x) / 10 ) = 6.0

    Now you only have to find the correct value for x, which is 37.5