Mysql specified range random number function rand () Tips for using _mysql

Source: Internet
Author: User

The formula is as follows:

RAND () * (y-x) + x

As we learn about PHP, a random function, Rand or Mt_rand, can pass in a parameter that produces a random integer between 0 and a parameter, or it can pass in two parameters, producing a random integer between the two parameters.
And in MySQL, the random number function Rand cannot pass the argument, generating 0 to 1 floating-point numbers, what if we need to produce a random integer greater than 1 in MySQL?
This demand is not unfamiliar, for example, I do the article system, need to cheat, to the article's browsing volume randomly plus a range of integers.

Now, suppose you need to generate a random integer between 234 and 5678, and how to implement it under MySQL.

We can't change the value of rand under MySQL, but we can change our needs,

1, we need the smallest is 234, the largest is 5678,rand produced the smallest is 0, the largest is 1, we need the number minus 234 to see?

The minimum number is 234-234 = 0, the maximum number is 5678-234 = 5444; Hey, highlights, the minimum number we need coincides with the minimum that Rand produces.
We just let the function produce 0 to 5444 random number, and then add 234, that's what we need.
We need to use a pseudo expression to describe the original, it will be
Rounding (rand (0,5444) + 234)

2, now just find a way to change our needs again, so that the minimum number of 0 unchanged, the maximum number of changes to 1,
Obviously, 5444 minus 5443 is 1, but in this case the minimum number will be negative.
To the minimum or 0, the maximum number is 1, too simple, 5444/5444 = 1, 0/5444 = 0
Now, the pseudo expression of the original requirement is:
Rounding (rand (0,1) * 5444 + 234)
3, the pseudo expression of the parameters removed, and MySQL under the same writing as Rand, the same effect. Take the whole function we use rounded round
So, the final true MySQL expression of our original needs is
ROUND (RAND () * 5444 + 234)

summarizes the idea:
1, comparing the differences between rand (x, y) and rand (0,1).
2, gradual rand (X,Y) transform to rand (0,1)
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 very simple mathematical arithmetic, with a simple example, the basic skills of some algorithms: reducing requirements so that the knowledge they have mastered to meet the requirements.

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.