MySQL rand () generates a random integer range and method

Source: Internet
Author: User

According to the official documentation, RAND () has a value range of [0,1]

In MySQL, you can perform the following commands to view:

Mysql>?RandName:'RAND'Description:syntax:RAND(),RAND(N)ReturnsA random floating-Point value VinchTherange 0 <= v < 1.0.IfaconstantintegerArgument N isspecified, it isUsed asThe seed Value,which produces aRepeatableSequence of column Values.inchthe Followingexample, note that the sequences of ValuesProduced by RAND(3) isthesame both placeswhereit occurs. Url:http://Dev.mysql.com/Doc/Refman/5.6/En/Mathematical-Functions.htmlExamples:mysql> CREATE TABLET (iINT); Query OK,0Rows Affected (0.42sec) MySQL> INSERT  intoTVALUES(1),(2),(3); Query OK,3Rows Affected (0.00sec) Records:3Duplicates:0Warnings:0MySQL> SELECTIRAND() fromT;+------+------------------+|I| RAND()|+------+------------------+|    1 | 0.61914388706828 ||    2 | 0.93845168309142 ||    3 | 0.83482678498591 |+------+------------------+3Rowsinch Set(0.00sec) MySQL> SELECTIRAND(3) fromT;+------+------------------+|I| RAND(3)|+------+------------------+|    1 | 0.90576975597606 ||    2 | 0.37307905813035 ||    3 | 0.14808605345719 |+------+------------------+3Rowsinch Set(0.00sec) MySQL> SELECTIRAND() fromT;+------+------------------+|I| RAND()|+------+------------------+|    1 | 0.35877890638893 ||    2 | 0.28941420772058 ||    3 | 0.37073435016976 |+------+------------------+3Rowsinch Set(0.00sec) MySQL> SELECTIRAND(3) fromT;+------+------------------+|I| RAND(3)|+------+------------------+|    1 | 0.90576975597606 ||    2 | 0.37307905813035 ||    3 | 0.14808605345719 |+------+------------------+3Rowsinch Set(0.01Sec

To get a random integer R in the I≤r≤j range, you need to use the expression floor (i + RAND () * (J–i + 1))
For example, to get a random integer within the range of 7 to 12, including 7 and 12, use the following statement:
SELECT Floor (7 + (RAND () * 6));

In addition, you can also use the round rounding function to achieve, taking into account that the first and last value probability will be unequal with the middle, so add 0.5来 to eliminate the difference in probability, to achieve a uniform distribution:

Generating [i,j] range positive integers:select round (rand () * (j-i+1) +i-0.5)

To get a random integer within the range of 7 to 12, including 7 and 12, use the following statement:
SELECT round (RAND () * 6+6.5));

If a negative integer is generated, it is possible to note that the leftmost value may exceed your requirements:

Mysql> SELECT round(-0.5);+-------------+| round(-0.5)|+-------------+|          -1 |+-------------+1Rowinch Set(0.00sec) MySQL> SELECT round(-0.4);+-------------+| round(-0.4)|+-------------+|           0 |+-------------+1Rowinch Set(0.00Sec

MySQL rand () generates a random integer range and method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.