How do I get random numbers in a data range via MySQL?
MySQL itself provides a function called Rand, which returns a V range of 0 <= v < 1.0.
The MySQL document that describes this function also describes the way that can be calculated by this formula floor (i + RAND () * (J–i)), which obtains the random number V of I <= v < J.
Attached Document Link: Http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand
For the sake of writing and invoking, write a function to return random numbers, note that this function in order to comply with personal habits, is to use floor (i + RAND () * (J–i + 1)) Such a formula, the range of random number V is i <= v <= J, rather than I <= v < J.
CREATE FUNCTION rand_num ( integer, integerRETURNSInteger BEGIN RETURNfloor+RAND*- +1)); END;
For example, to get a random number of 1-9, this is called:
Select rand_num (19);
MySQL definition function rand