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

  • SEARCH
  • Home
  • 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 6750785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:50:59+00:00 2026-05-26T12:50:59+00:00

As Mysql rand() is time consuming I am using alternate way using Mysql max()

  • 0

As Mysql rand() is time consuming I am using alternate way using Mysql max() and PHP. I wrote this code for fetching random product_id’s:

function RandomUniqueArray($min,$max,$limit){

    $random_array = array();
    if(isset($limit) && is_numeric($limit)){
        for($i=0;$i<$limit;){
            $rand_val = mt_rand($min, $max);
            if(!in_array($rand_val, $random_array)){
                $random_array[] = $rand_val;
                $i++;
            }
        }
    }
    return $random_array;
}

This works fine as I want each time it gives different result set with different unique values but it takes 6.232 micro seconds.
Ohter I got by Google is:

$random_array = array_rand(array_fill($min,$max, true),$limit);

with takes only 0.101 microseconds but its result set is repeated means. Unique values array is fine but whole set is repeated. Why is it so???

I called both of these by one by one as

$random_array = RandomUniqueArray(1,64000,4);

And

$random_array = array_rand(array_fill(1,64000, true),4);

Thank you.

  • 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. Editorial Team
    Editorial Team
    2026-05-26T12:50:59+00:00Added an answer on May 26, 2026 at 12:50 pm

    I made a script,that only takes ̣̣̣+- 4.5E-6.

    Try it.

    function randomValue($min,$max,$limit)
    {
        $array = Array();
        $MAX = mt_rand($min,$max);
        for($i = 0;$i < $limit;$i++)
        {
            $array[$i] = mt_rand($min,$MAX);
            while( is_array($array[$i],$array) ) //To check if exist, if. Make new.
            {
               $array[$i] = mt_rand($min,$MAX);
            }
        }   
        return $array;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does SQLite support seeding the RANDOM() function the same way MySQL does with RAND()
Using PHP and MySQL, is there a way to use a different ORDER BY
MySQL what's the best way to select X random entries (rather than just one)
Right now I'm selecting 3 random rows from a table using ORDER BY Rand();
according to this article: http://net.tutsplus.com/tutorials/other/top-20-mysql-best-practices/ using ORDER BY RAND() is a bad idea because:
Need help with PHP Mysql code to insert multiple select data into database. With
I'm using PHP with a replicated MySQL database. I want to do some database
I have this code: $result = mysql_query(SELECT * FROM quote ORDER BY RAND() LIMIT
The MYSQL rand() function is notoriously slow in select statements, is this true for
I'm trying to generate select names from a mysql database by using the rand()

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.