Tips for using the random number function rand () in the specified range of MySQL, mysqlrand

Source: Internet
Author: User

Tips for using the random number function rand () in the specified range of MySQL, mysqlrand

The formula is as follows:

Rand () * (y-x) + x

All php users know that the random function rand or mt_rand can input a parameter to generate a random integer between 0 and the parameter. You can also input two parameters, generates a random integer between the two parameters.
In mysql, the random number function rand cannot pass parameters. The resulting floating point number between 0 and 1. What should we do if we need to generate a random integer greater than 1 in mysql?
Such a requirement is no stranger. For example, the article system we are working on requires cheating. We randomly add integers in a certain range to the page views of the article.

Now, suppose we need to generate a random integer between 234 and 5678, how can we implement it in mysql.

We cannot change the rand value in mysql, but we can change our requirements,

1. We need to have a minimum value of 234, a maximum value of 5678, a minimum value of 0 produced by rand, and a maximum value of 1. What can we do if we need a number minus 234?

The minimum number is 234-234 = 0, and the maximum number is 5678-234 = 5444. Hey, the minimum number we need is the same as the minimum number produced by rand.
We just need to let the function generate a random number ranging from 0 to 5444, and then add 234, which is our original requirement.
We used a pseudo expression to describe it.
Round (rand (234) +)

2. Now we only need to find a way to change our needs, so that the minimum number is 0 and the maximum number is 1,
Obviously, 5444 minus 5443 is 1, but in this way, the minimum number will be negative.
The minimum number or 0, and the maximum number is 1. It is too simple. 5444/5444 = 1, 0/5444 = 0.
Currently, the original pseudo expression is:
Round (rand (5444) * 234 +)
3. Remove the parameter of the pseudo expression, which is the same as rand in mysql. We use rounding the entire function into ROUND.
Therefore, the final mysql expression we originally needed is
ROUND (RAND () * 5444 + 234)

1. Compare the differences between rand (x, y) and rand (0, 1.
2. gradually convert rand (x, y) to rand ()
Rand (x, y)
= Rand (0, (y-x) + x
= Rand (0/(y-x), (y-x)/(y-x) * (y-x) + x
= Rand () * (y-x) + x

This is a simple mathematical and arithmetic expression. I use a simple example to explain some basic algorithm skills: lowering the requirements allows your knowledge to meet your needs.

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: 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.