Question: I want to get random number between 65 to 122.I am using this query   

```
SELECT (FLOOR(65 + RAND() * 122));

```

after getting help from [this question](https://stackoverflow.com/questions/14865632/mysql-update-with-random-number-between-1-3) but it is giving me numbers outside the range of 65-122.  

Please help.Thanks

Answer: 
```
SELECT (FLOOR(65 + RAND() * 57));

```

Rand provides a number between 0-1. Like 0,75.
So if you want to get a number betwen 65 and 122 then:

```
65 = 65+0*57
122 = 65+1*57

```
`RAND()` returns a random number between 0 and 1, hence `65 + RAND() * 122` is equal to minimum/maximum:

```
MIN: 65 + 0 * 122 = 65 + (0 * 122) = 65 + 0 = 65
MAX: 65 + 1 * 122 = 65 + (1 * 122) = 65 + 122 = 187

```

Hence what you actually want `57` instead of `122` as `122 - 65 = 57`.

Now the minimum/maximum is:

```
MIN: 65 + 0 * 57 = 65 + (0 * 57) = 65 + 0 = 65
MAX: 65 + 1 * 57 = 65 + (1 * 57) = 65 + 57 = 122

```

---

Alternately, here's a custom function you can use:

```
DELIMITER //

CREATE FUNCTION RAND_BETWEEN(min FLOAT, max FLOAT) 
    RETURNS FLOAT
    BEGIN
        RETURN min + ((max - min) * RAND());
    END//

DELIMITER ;

```

Usage:

```
SELECT RAND_BETWEEN(60, 120)        # eg. 115.81029510498047
SELECT RAND_BETWEEN(0, 150)         # eg. 62.729270935058594
SELECT RAND_BETWEEN(10000, 10005)   # eg. 10004.9560546875
SELECT FLOOR( RAND_BETWEEN(5, 10) ) # eg. 6

```