lundi 6 mars 2017

How to generate true random numbers on Mysql

This is probably a very basic question and has been asked hundreds of times, but I looked for a solution and couldn't find any.

I have a table that has 1M records. I want to update the price column by generating a total random price from 1$ to 5000$ for each row. This is how I do it:

update `table` set price = FLOOR(1 + RAND() * 5000);

Interestingly, when I get the AVG(price) from some specific WHERE Clauses I always get a number = ~5000/2. For example, if price = FLOOR(1 + RAND() * 5000) then AVG(price) = ~2500$, if price = FLOOR(1 + RAND() * 2500) then AVG(price) = ~1250$, and so on. Any idea where the problem is? Thanks.




Aucun commentaire:

Enregistrer un commentaire