How to Ensure Unique Random Numbers Without Repeats in PHP?

Barbara Streisand
Release: 2024-10-21 11:58:02
Original
797 people have browsed it

How to Ensure Unique Random Numbers Without Repeats in PHP?

Generating Random Numbers Without Repeats

Your current method of generating random Yelp listings using rand() function does not ensure unique listings. To prevent duplicates, consider implementing a smarter randomization strategy.

One approach is to use the native shuffle() function on a range of numbers representing the listing indices. This generates a shuffled array without duplicates:

<code class="php">$numbers = range(0, 19);
shuffle($numbers);</code>
Copy after login

An alternative method is to create a custom randomGen() function that accepts parameters for the minimum, maximum, and desired quantity of random numbers:

<code class="php">function randomGen($min, $max, $quantity) {
    $numbers = range($min, $max);
    shuffle($numbers);
    return array_slice($numbers, 0, $quantity);
}</code>
Copy after login

To implement this in your PHP script, generate a random listing ID using randomGen() and store it in a database table. On each page refresh, check if the generated listing ID matches the stored value. If not, display that listing and update the table with the new ID.

This approach ensures that all 20 listings are displayed once before any repeats occur. Here's an updated code snippet:

<code class="php"><?php

$businesses = json_decode($data);

$db = new PDO('mysql:host=localhost;dbname=yelp_listings', 'root', 'password');

// Generate a random listing ID using randomGen()
$listing_id = randomGen(1, 20, 1)[0];

// Check if the listing ID matches the stored value
$stmt = $db->prepare('SELECT listing_id FROM shown_listings WHERE id = ?');
$stmt->execute([$listing_id]);

// Display the listing if it hasn't been shown yet
if ($stmt->rowCount() == 0) {
    $business = $businesses->businesses[$listing_id - 1];

    echo "<img border=0 src='" . $business->image_url . "'><br/>";
    echo $business->name . "<br/>";
    echo "<img border=0 src='" . $business->rating_img_url_large . "'><br/>";

    // Add the listing ID to the shown_listings table
    $stmt = $db->prepare('INSERT INTO shown_listings (id) VALUES (?)');
    $stmt->execute([$listing_id]);
}

?></code>
Copy after login

The above is the detailed content of How to Ensure Unique Random Numbers Without Repeats in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!