How to use the random number function rand () in MySQL
Source: Internet
Author: User
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. 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 is required.
What should I do if mysql generates a random integer greater than 1? 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)
Summary:
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.
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.