UPDATE all records with a random number from a range

Try this:

UPDATE tableName SET columnName = FLOOR( 1 + RAND( ) *3 );

From the MySQL documentation for RAND:

Returns a random floating-point value v in the range 0 <= v < 1.0.

So in the above query, the largest value which could be generated by 1 + RAND()*3 would be 3.999999, which when floored would give 3. The smallest value would occur when RAND() returns 0, in which case this would give 1.

Leave a Comment