I'm writing an e-commerce website for school and i want to populate it with a couple thousand reviews, reclamations, orders, users, etc.
Reviews of a product include an integer score (1-5), and the product page displays an average score for each products
In my Review factory for the seeder i just have
public function definition()
{
return[
'user_id' => User::all()->random()->id,
'book_id' => Book::all()->random()->id,
'text' => fake()->paragraph,
'score' => rand(1,5),
];
}
This works fine except that, since i'm generating over 10k reviews, all my products average out to an average score of 3 ( Average score works fine, it's not a bug ).
Question: How would i go about generating random scores that wouldn't average out ?
CodePudding user response:
You could use some property of the book to generate different score ranges.
Here's an example with the book ID:
public function definition()
{
$book = Book::all()->random();
if ($book->id % 2 === 1) {
$score = random_int(3, 5); // better score
} else {
$score = random_int(1, 3); // worse score
}
return[
'user_id' => User::all()->random()->id,
'book_id' => $book->id,
'text' => fake()->paragraph,
'score' => $score,
];
}
Assuming the book ID is an integer sequence, half the books should have a "bad to medium" score and the rest will have "medium to good". You can increase the modulus to program in more cases.