Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate 6 digit random number in mysql

Tags:

sql

mysql

I want to generate random 6 digit number in mysql but sometime it generate only 5 digit.

UPDATE member SET updates = FLOOR(RAND() * 999999)

like image 729
Mehur Avatar asked Oct 30 '16 16:10

Mehur


People also ask

How do you generate a 6 digit random number in SQL?

One simple method is to make use of RAND() function available in SQL Server. RAND() function simply generate a decimal number between 0 (inclusive) and 1 (exclusive). The logic is to multiply value returned by RAND() by 1 billion so that it has enough digits and apply LEFT function to extract 6 digits needed.

How to generate a random number in MySQL?

MySQL RAND() Function The RAND() function returns a random number between 0 (inclusive) and 1 (exclusive).

How can we get a random number between 1 and 100 in MySQL?

To get the random value between two values, use MySQL rand() method with floor(). The syntax is as follows. select FLOOR( RAND() * (maximumValue-minimumValue) + minimumValue) as anyVariableName; Let us check with some maximum and minimum value.

How do you give a random number a variable in C++?

One way to generate these numbers in C++ is to use the function rand(). Rand is defined as: #include <cstdlib> int rand(); The rand function takes no arguments and returns an integer that is a pseudo-random number between 0 and RAND_MAX.


1 Answers

If the problem is that you are missing leading zeros, you can left pad with spaces:

UPDATE member
    SET updates = LPAD(FLOOR(RAND() * 999999.99), 6, '0');

I hope you understand that "random" means "random" and different rows can get the same value.

like image 175
Gordon Linoff Avatar answered Oct 20 '22 13:10

Gordon Linoff