Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 151599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:28:31+00:00 2026-05-11T09:28:31+00:00

I want to show a random record from the database. I would like to

  • 0

I want to show a random record from the database. I would like to be able to show X number of random records if I choose. Therefore I need to select the top X records from a randomly selected list of IDs

(There will never be more than 500 records involved to choose from, unless the earth dramatically increases in size. Currently there are 66 possibles.)

This function works, but how can I make it better?

/***************************************************/ /* RandomSite */ //****************/ //  Returns an array of random site IDs or NULL /***************************************************/    function RandomSite($intNumberofSites = 1) {     $arrOutput = NULL;     //open the database     GetDatabaseConnection('dev');      //inefficient     //$strSQL = 'SELECT id FROM site_info WHERE major <> 0 ORDER BY RAND() LIMIT '.$intNumberofSites.';';      //Not wonderfully random     //$strSQL = 'SELECT id FROM site_info WHERE major <> 0 AND id >= (SELECT FLOOR( COUNT(*) * RAND()) FROM site_info ) ORDER BY id LIMIT '.$intNumberofSites.';';      //Manual selection from available pool of candidates  ?? Can I do this better ??     $strSQL = 'SELECT id FROM site_info WHERE major <> 0;';      if (is_numeric($intNumberofSites))     {         //excute my query         $result = @mysql_query($strSQL);         $i=-1;          //create an array I can work with  ?? Can I do this better ??         while ($row = mysql_fetch_array($result, MYSQL_NUM))         {             $arrResult[$i++] = $row[0];         }          //mix them up         shuffle($arrResult);          //take the first X number of results  ?? Can I do this better ??         for ($i=0;$i<$intNumberofSites;$i++)         {             $arrOutput[$i] = $arrResult[$i];         }     }         return $arrOutput;     } 

UPDATE QUESTION: I know about the ORDER BY RAND(), I just don’t want to use it because there are rumors it isn’t the best at scaling and performance. I am being overly critical of my code. What I have works, ORDER BY RAND() works, but can I make it better?

MORE UPDATE There are holes in the IDs. There is not a ton of churn, but any churn that happens needs to be approved by our team, and therefore could handled to dump any caching.

Thanks for the replies!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:28:31+00:00Added an answer on May 11, 2026 at 9:28 am

    Why not use the Rand Function in an orderby in your database query? Then you don’t have to get into randomizing etc in code…

    Something like (I don’t know if this is legal)

    Select * from site_info Order by Rand() LIMIT N 

    where N is the number of records you want…

    EDIT
    Have you profiled your code vs. the query solution? I think you’re just pre-optimizing here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a number of images in my directory. I want to show random
GUID is big random number show in a HEX basis. I want to show
We want to show a hint for a JList that the user can select
I want to show an image from an URL with a certain width and
I'm trying to select a random 10% sampling from a small table. I thought
I am looking for a way to show a random nvarchar field from a
I want the messagebox to only show if the number is equal to 0.
Possible Duplicate: Simple Random Samples from a (My)Sql database Hi! Let say that I
I want to show a new window transition from bottom and transition to bottom
I want to show HTML content inside Flash. Is there some way to do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.