Reputation: 95
is there any way by which we can generate a 1 million random numbers of length four between 0-9999? Then i have to store them in a MySql DB, Language which i am using is Java.
Upvotes: 0
Views: 146
Reputation: 521209
MySQL (excluding MariaDB) does not seem to support any built in sequence generating capability. Given that you are using Java, I might actually handle your problem there.
If you want a MySQL solution, then here is one option. We can generate an on-the-fly sequence of length 1 million, using a numbers table/CTE with cross joins. If you are using < MySQL 8, then just replace the CTE with an inlined table.
WITH digits AS (
SELECT 0 AS digit UNION ALL
SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL
SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL
SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
)
INSERT INTO rand_table (val)
SELECT RAND()*10000
FROM digits d1
CROSS JOIN digits d2
CROSS JOIN digits d3
CROSS JOIN digits d4
CROSS JOIN digits d5
CROSS JOIN digits d6;
Upvotes: 1