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 6098419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:09:22+00:00 2026-05-23T13:09:22+00:00

I have two columns in a row: min_value , max_value . Is there a

  • 0

I have two columns in a row: min_value, max_value. Is there a way to do a select like:

SELECT RAND(`min_v`, `max_v`) `foo` [..]

I do realize that RAND does a different thing; the closest I came up (with help) is (RAND() * (max-min))+min, though it will produce a float number, which I’d need then to ROUND() and this is just completely wrong.

Unless anyone can suggest an alternative (which would be very useful), I will go PHP way.

  • 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-23T13:09:22+00:00Added an answer on May 23, 2026 at 1:09 pm

    Actually, ROUND((RAND() * (max-min))+min) is the best way in MySQL to do what you’d like. It is also the best way in ActionScript, JavaScript, and Python. Honestly, I prefer it to the PHP way because it is more convenient.

    Because I don’t know how many rows you’ll be returning, I can’t advise you whether it is better to use PHP or MySQL for this, but if you’re dealing with a large number of values you probably are better off using MySQL.

    Addendum


    So, there was a question as to whether this is better in PHP or MySQL. Instead of getting into a debate on principles, I ran the following:

    <pre><?php
    
    $c = mysql_connect('localhost', 'root', '');
    
    if(!$c) die('!');
    echo mysql_select_db('test', $c)?'Connection':'Failure';
    echo PHP_EOL;
    
    echo ':::::::::::::::::::::::::BEGINNING MYSQL RAND::::::::::::::::::::::::::::::'.PHP_EOL;
    $start = microtime(1);
    for( $i = 0; $i < 100000; $i++ )
    {
        $r = mysql_query( 'SELECT ROUND(RAND() * (200-10) + 10) FROM dual' );
        $r = mysql_fetch_array( $r );
    }
    $end = microtime(1);
    
    echo  ($end - $start) . " for MySQL select".PHP_EOL;
    
    echo ':::::::::::::::::::::::::BEGINNING PHP RAND::::::::::::::::::::::::::::::' .PHP_EOL;
    $start = microtime(1);
    for( $i = 0; $i < 100000; $i++ )
    {
        $r = mysql_query( 'SELECT 200 AS two, 10 AS tem FROM dual' );
        $r = mysql_fetch_array( $r );
        $r[2]= rand($r[0], $r[1]);
    }
    $end = microtime(1);
    
    echo  ($end - $start) . " for PHP select".PHP_EOL;
    

    MySQL is faster by about 2-3%.

    If you use this, however (note, more columns return by MySQL):

    <pre><?php
    
    $c = mysql_connect('localhost', 'root', '');
    
    if(!$c) die('!');
    echo mysql_select_db('test', $c)?'Connection':'Failure';
    echo PHP_EOL;
    
    echo ':::::::::::::::::::::::::BEGINNING MYSQL RAND::::::::::::::::::::::::::::::'.PHP_EOL;
    $start = microtime(1);
    for( $i = 0; $i < 100000; $i++ )
    {
        $r = mysql_query( 'SELECT ROUND(RAND() * (200-10) + 10) as rd, 200 as two, 10 as ten FROM dual' );
        $r = mysql_fetch_array( $r );
    }
    $end = microtime(1);
    
    echo  ($end - $start) . " for MySQL select".PHP_EOL;
    
    echo ':::::::::::::::::::::::::BEGINNING PHP RAND::::::::::::::::::::::::::::::' .PHP_EOL;
    $start = microtime(1);
    for( $i = 0; $i < 100000; $i++ )
    {
        $r = mysql_query( 'SELECT 200 AS two, 10 AS tem FROM dual' );
        $r = mysql_fetch_array( $r );
        $r[2]= rand($r[0], $r[1]);
    }
    $end = microtime(1);
    
    echo  ($end - $start) . " for PHP select".PHP_EOL;
    

    MySQL comes out behind by 3-4% (very inconsistent results) (about the same results if you don’t use an array index assignment for $r[2]).

    The major difference, it seems, comes from the number of records return to PHP and not the randomization system itself. So, if you need column A, column B, and a random value, use PHP. If you only need the random value, then use MySQL.

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

Sidebar

Related Questions

I have a table with two columns and one row, and 100% width across
I have two columns in Excel, like this: A B 0.5 0.4 0.6 0.59
I have the following two columns: SELECT b.ip_address AS IP ,b.mask AS MASK FROM
I have two columns in excel like the following a,apple a,bannana a,orange a,plum b,apple
I have a table that currently has two columns per row and I need
I have a Grid as a root container with two columns defined (There's only
I have a table called Foo and I have two columns: Lorem and Ipsum,
i have two columns that store values(numbers), how do i select where my given
Suppose I have two matrices, each with two columns and differing numbers of row.
I have two columns. ColA and ColB contains char(10) with data 20090520 and 20090521.

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.